Sharing the Code

Programming stuff that might be useful to others

Calling super and this in C# constructors

Coming from Java it took me a while to find out how to call other constructors in a constructor.

To call an ancestor’s constructor:

[sourcecode language=’c#’]
public MyClass(string param):base(param) {
}
[/sourcecode]

To call a constructor in the same class:

[sourcecode language=’c#’]
public MyClass(string param):this(){
}

public MyClass(){
}
[/sourcecode]

Static constructor:

[sourcecode language=’c#’]
static string sField;

static MyClass(){
sField=”initialized”;
}
[/sourcecode]

Comments are closed