32

Below is the constructor of a struct named Complex with two member variables, Real and Imaginary:

public Complex(double real, double imaginary) : this()
{
 Real = real;
 Imaginary = imaginary;
}

What is the use of the part after the colon in the function header?

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
ankit0311
  • 735
  • 3
  • 10
  • 20

7 Answers7

39

You can always make a call to one constructor from within another. Say, for example:

public class mySampleClass
{
    public mySampleClass(): this(10)
    {
        // This is the no parameter constructor method.
        // First Constructor
    }

    public mySampleClass(int Age) 
    {
        // This is the constructor with one parameter.
        // Second Constructor
    }
}

this refers to same class, so when we say this(10), we actually mean execute the public mySampleClass(int Age) method. The above way of calling the method is called initializer. We can have at the most one initializer in this way in the method.

In your case it going to call default constructor without any parameter

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
28

It's called constructor chaining - where it's in fact calling another constructor (which takes no params in this case), and then comes back and does any additional work in this constructor (in this case setting the values of Real and Imaginary).

Bridge
  • 29,818
  • 9
  • 60
  • 82
19

This is a constructor-initializer which invokes another instance constructor immediately before the constructor-body. There are two forms of constructor initializers: this and base.

base constructor initializer causes an instance constructor from the direct base class to be invoked.

this constructor initializer causes an instance constructor from the class itself to be invoked. When constructor initializer does not have parameters, then parameterless constructor is invoked.

class Complex
{
   public Complex() // this constructor will be invoked
   {    
   }

   public Complex(double real, double imaginary) : this()
   {
      Real = real;
      Imaginary = imaginary;
   }
}

BTW usually constructors chaining is done from constructors with less parameters count to constructors with more parameters (via providing default values):

class Complex
{
   public Complex() : this(0, 0)
   {    
   }

   public Complex(double real, double imaginary)
   {
      Real = real;
      Imaginary = imaginary;
   }
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
3

The : this() instructs the compiler to call the class's default constructor before executing the code found in this constructor. If the default constructor is empty, it has no actual effect.

Steve Czetty
  • 6,147
  • 9
  • 39
  • 48
3

It is called constructor chaining. Using it in a constructor to call another constructor. You can also do it for class inheritance, for example:

public class MyException : Exception
{
    public MyException(string message) : base(message){}
}
  • 1
    Please be sure to review the existing answers before contributing a new one. In this case, several answers have mentioned constructor chaining, and while the accepted answer (from eight years ago) doesn't mention it by name, it clearly demonstrates the concept. – Jeremy Caney Jul 03 '20 at 18:50
  • 1
    If you read carefully, you will see my example contribute with different approach for superclass construction chaining. Thanks for your comment. – Erisvaldo Junior Jan 22 '21 at 19:11
0

Its constructor chaining which calls default or parameterless constructor of the same class.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
0

The call to the default constructor is useful (and required) when defining a struct that uses the C# shorthand notation for properties. For example:

public struct Foo
{
    public int X{get;set;}

    public Foo(int x)
    {
        X = x;
    }
}

The compiler will raise an error here because the 'this' object cannot be used within the constructor to assign X until all of the structs fields are assigned. Behind the scenes, a field is automatically created as a backing store for the property X, and calling the default constructor for the struct initializes this field (to the default value of 0 in this case).

See this question for further details: Automatically implemented property in struct can not be assigned

Community
  • 1
  • 1
Michael Petito
  • 12,891
  • 4
  • 40
  • 54