function() : this(null) {}
Asked Answered
A

4

6

Can someone please explain the following syntactic sugar?

protected MyConstructor() : this(null)

Mainly I am interested in this part: ": this(null)"

I know how protected, constructors and "this" keyword work, but am confused and cannot find any detailed information of the last part all put-together in all my online searches.

Edit: I should add that it is in a public abstract class. So I guess the constructor is calling the implementers constructor.

Thanks

Amongst answered 15/1, 2014 at 17:27 Comment(5)
Guess is that it calls another constructor and passes in the value null. Is there a constructor that takes one parameter?Yugoslav
Did you debug the code to try to see what would happen?Streusel
By the way - if you are familiar with the "constructor() : base()" syntax, this should look familiar. Instead of calling the super class constructor this calls the constructor in the current class.Yugoslav
Look into constructor overloadingKero
There's no sugar thereBitty
P
9

It calls another class constructor that has a parameter:

protected MyConstructor() : this(null) { }  // This calls the other constructor

protected MyConstructor(object whatever)
{
    Frob(whatever);
}
Phonometer answered 15/1, 2014 at 17:29 Comment(0)
L
8

This is a special syntax for constructors. You can have two basic variants:

protected MyConstructor() : this(null)

Calls the different overload of the constructor with the null parameter.

protected MyConstructor() : base(null)

Calls the constructor on a base class with the null parameter.

So, you can have a class like this:

class MyClass
{
  object someObject;

  public MyClass() : this(null) {}
  public MyClass(object someObject) { this.someObject = someObject; }
}

Now you can instantiate the class like this:

var c = new MyClass(); // c.someObject is null
var c2 = new MyClass(new object()); // c2.someObject is some new object instance

This is required because you can't reuse constructor code in any other way. If you were just overriding or overloading a method, the equivalent would look like this:

public void DoStuff()
{
  DoStuff(null);
}

public void DoStuff(object someObject)
{
  // Do some stuff
}
Lavernelaverock answered 15/1, 2014 at 17:29 Comment(2)
Just for kicks, I'll note you can write the last example as: public void DoStuff(object someObject = null) { // Do some stuff }Stalnaker
@MikeChristensen True, but with one important distinction - optional parameters are determined at compile time. So if you have a library, that changes an optional parameter's default value, the change will not propagate to users of the library until they recompile. That's because optional parameters aren't actually a feature of the IL, they're just syntactic sugar.Lavernelaverock
S
5

There is another constructor on that same object, which takes some sort of nullable object. For example:

public MyConstructor(string str)
{
   // A
}


public MyConstructor() : this(null)
{
   // B
}

In this example (changing constructors to public for demonstration purposes), if I call:

var newObj = new MyConstructor();

It will create a MyConstructor object, run the code in A first (passing in null as the parameter) then run the code in B next.

This is a way of allowing you to consolidate common code that needs to run regardless of what constructor is called.

Stalnaker answered 15/1, 2014 at 17:29 Comment(0)
S
3

What this(null) is doing is calling another constructor in the same class that takes one parameter, and passing a null value as that parameter. Look into constructor overloading for more info. Also, constructor chaining is more pertinent to your question, but I'd look into both topics.

Spiderwort answered 15/1, 2014 at 17:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.