Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

super statement in C#

Tags:

c#

exception

I'm creating a class to manage exception in c#, and I'd like to create some constructor methods which recalls the superclass; this is the definition of my class:

class DataSourceNotFoundException: System.Exception 

but since in c# there is no super method what am I supposed to call to get the constructor methods of System.Exception?

like image 887
Dharma Dude Avatar asked Jul 22 '11 08:07

Dharma Dude


People also ask

What is a super loop in C?

Definition. A super loop is a program structure comprised of an infinite loop, with all the tasks of the system contained in that loop.

What is super loop problem?

Because there are no operating system to return to or an embedded device is running until the power supply is removed. So, to run set of statements, we need a loop that must not be finished, such kind of loops are known as 'Super Loop' or 'Infinite Loop'.

What is used for monitoring the firmware execution?

The watchdog timer is used for monitoring the firmware execution which is a hardware timer.


2 Answers

You call a parent constructor using base before the body of the constructor:

public class FooException : Exception
{
    public FooException(string message) : base(message)
    {
    }
}

Obviously you don't have to just pass a parameter from your own constructor up as an argument to the base constructor:

public class FooException : Exception
{
    public FooException(int x) : base("Hello")
    {
        // Do something with x
    }
}

The equivalent to chain to a constructor in the current class is to use this instead of base.

Note that constructor chaining works very slightly differently in C# compared with Java, with respect to when instance variable initializers are run. See my article on C# constructors for more details.

like image 144
Jon Skeet Avatar answered Oct 02 '22 19:10

Jon Skeet


In general the keyword base is what you want. Check http://msdn.microsoft.com/en-us/library/hfw7t1ce(v=vs.71).aspx

like image 21
Paweł Obrok Avatar answered Oct 02 '22 18:10

Paweł Obrok