Invoking an overloaded constructor using this keyword in C#

Prerequisite : Constructors in C#

C# provides a powerful keyword known as this keyword and this keyword has many usages. Here we use this keyword to call an overloaded constructor from another constructor.

Important Points:

  • When you use this keyword to call a constructor, the constructor should belong to the same class.
  • You can also pass parameter in this keyword.
  • This keyword always pointing to the members of the same class in which it is used.
  • When you use this keyword, it tells the compiler to invoke the default constructor. Or in other words, it means a constructor that does not contain arguments.

    Syntax:

    class X 
    {
    
       public X: this()
       {
    
          // Code..
    
       }
    }
    
  • this keyword contains the same type and the same number of parameters that are present in the calling constructor.

    Syntax:

    class X
    {
       public X(int x): this(int)
       {
    
           // Code..
       }
    }
    
  • This concept removes the assignment of replication of properties in the same class.

Below programs illustrate how to call the overloaded constructor using this keyword:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate how to invoke
// overloaded constructor using this keyword
using System;
class Geek {
  
    // Constructor without parameter
    public Geek()
    {
        Console.WriteLine("Hello! Constructor 1");
    }
  
    // Constructor with parameter
    // Here this keyword is used 
    // to call Geek constructor
    public Geek(int a) 
    : this()
    {
        Console.WriteLine("Hello! Constructor 2");
    }
}
  
// Driver Class
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Create oject of Geek class
        Geek obj = new Geek(2);
    }
}

chevron_right


Output:

Hello! Constructor 1
Hello! Constructor 2

Explanation: In the above example, Geek class contains two constructors, i.e, Geek() is without parameter and Geek(int a) is with parameter. Now we call Geek() constructor in Geek(int a) by using this() keyword. Here this() keyword does not contain any argument because the constructor does not contain any parameter.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate how to invoke
// overloaded constructor using this keyword
using System;
class Geek {
  
    // Constructor with parameters
    public Geek(int a, double b, string c)
    {
        Console.WriteLine(a);
        Console.WriteLine(b);
        Console.WriteLine(c);
    }
  
    // Constructor with parameters
    // Here this keyword is used 
    // to call Geek constructor
    public Geek(int a, int b)
        : this(50, 2.9, "Hello")
    {
        Console.WriteLine(a);
        Console.WriteLine(b);
    }
}
  
// Driver Class
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Create oject of Geek class
        Geek obj = new Geek(15, 30);
    }
}

chevron_right


Output:

50
2.9
Hello
15
30

Explanation: In the above example, Geek class contains two constructors, i.e, Geek(int a, double b, string c) and Geek(int a, int b) and both are parameterized constructors. Now we call Geek(int a, double b, string c) constructor in Geek(int a, int b) by using this(50, 2.9, "Hello") keyword. Here this(50, 2.9, "Hello") keyword contains the same number and type of argument that are present in the Geek(int a, double b, string c) constructor.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.