calling base constructor passing in a value

c#

Solution

The base call is always done first, but you can make it call a static method. For example:

public Constructor(string x) : base(Foo(x))
{
    // stuff
}

private static string Foo(string y)
{
    return y + "Foo!";
}

Now if you call

new Constructor("Hello ");

then the base constructor will be called with "Hello Foo!".

Note that you cannot call instance methods on the instance being constructed, as it's not "ready" yet.

Problem

``` public DerivedClass(string x) : base(x) { x="blah"; } ``` will this code call the base constructor with a value of x as "blah"?

Original source