C# Constructors with Examples

In c#, Constructor is a method that will invoke automatically whenever an instance of class or struct is created. The constructor will have the same name as the class or struct, and it is useful to initialize and set default values for the data members of the new object.

 

If we create a class without any constructor, the compiler will automatically generate one default constructor for that class. So, there is always one constructor that will exist in every class.

 

In c#, a class can contain more than one constructor with different types of arguments. The constructors will never return anything, so we don’t need to use any return type, not even void while defining the constructor method in the class.

C# Constructor Syntax

As discussed, the constructor is a method, and it won’t contain any return type. If you want to create a constructor in c#, you need to create a method with the class name.

 

Following is the syntax of creating a constructor in the c# programming language.

 

public class User
{
// Constructor
public User()
{
// Your Custom Code
}
}

If you observe the above syntax, we created a class called “User” and a method whose name is same as the class name. Here the method User() will become a constructor of our class.

C# Constructor Types

In c#, we have different types of constructors available; those are

 

Now we will learn about each constructor in a detailed manner with examples in the c# programming language.

C# Default Constructor

In c#, if we create a constructor without any parameters, we will call it a default constructor. Every instance of the class will be initialized without any parameter values.

 

Following is the example of defining the default constructor in the c# programming language.

 

using System;

namespace Tutlane
{
     class User
     {
         public string name, location;
         // Default Constructor
         public User()
         {
            name = "Suresh Dasari";
            location = "Hyderabad";
         }
     }
     class Program
     {
         static void Main(string[] args)
         {
            // The constructor will be called automatically once the instance of the class created
            User user = new User();
            Console.WriteLine(user.name);
            Console.WriteLine(user.location);
            Console.WriteLine("\nPress Enter Key to Exit..");
            Console.ReadLine();
         }
     }
}

If you observe the above example, we created a class called “User” and the constructor method “User()” without having any parameters. When we create an instance of our class (User), our constructor method will automatically be called.

 

When you execute the above c# program, you will get the result below.

 

C# Default Constructor Example Result

 

If you observe the above result, our constructor method has called automatically and initialized the parameter values after creating an instance of our class.

C# Parameterized Constructor

In c#, if we create a constructor with at least one parameter, we will call it a parameterized constructor. Every instance of the class will be initialized with parameter values.

 

Following is the example of defining the parameterized constructor in the c# programming language.

 

using System;

namespace Tutlane
{
    class User
    {
        public string name, location;
        // Parameterized Constructor
        public User(string a, string b)
        {
           name = a;
           location = b;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // The constructor will be called automatically once the instance of the class created
            User user = new User("Suresh Dasari", "Hyderabad");
            Console.WriteLine(user.name);
            Console.WriteLine(user.location);
            Console.WriteLine("\nPress Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

If you observe the above example, we created a class called “User” and the constructor method “User(string, string)” with parameters. When we create an instance of our class (User) with the required parameters, our constructor method will automatically be called.

 

When you execute the above c# program, you will get the result below.

 

C# Parameterized Constructor Example Result

 

If you observe the above result, our constructor method has called automatically and initialized the parameter values after creating an instance of our class with the required parameters.

 

In the next chapters, you will learn about copy, static, and private constructors in the c# programming language with examples.

C# Constructor Overloading

In c#, we can overload the constructor by creating another constructor with the same method name but with different parameters.

 

Following is the example of implementing a constructor overloading in the c# programming language.

 

using System;

namespace Tutlane
{
    class User
    {
        public string name, location;
        // Default Constructor
        public User() {
             name = "Suresh Dasari";
             location = "Hyderabad";
        }
        // Parameterized Constructor
        public User(string a, string b)
        {
             name = a;
             location = b;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
           User user = new User(); // Default Constructor will be called
           User user1 = new User("Rohini Alavala", "Guntur"); // Parameterized Constructor will be called
           Console.WriteLine(user.name + ", " + user.location);
           Console.WriteLine(user1.name + ", " + user1.location);
           Console.WriteLine("\nPress Enter Key to Exit..");
           Console.ReadLine();
        }
    }
}

If you observe the above example, we created a "User" class. We overloaded a constructor “User()” by creating another constructor “User(string, string)” with the same name but with different parameters.

 

When you execute the above c# program, you will get the result below.

 

C# Constructor Overloading Example Result

 

If you observe the above result, the respective constructor methods will call automatically when we create an instance of our class with or without parameters based on our requirements.

C# Constructor Chaining

In c#, Constructor Chaining is an approach to invoke one constructor from another constructor. To achieve constructor chaining, we need to use this keyword after our constructor definition.

 

Following is the example of implementing a constructor chaining in c# programming language.

 

using System;

namespace Tutlane
{
     class User
     {
         public User()
         {
             Console.Write("Hi, ");
         }
         public User(string a): this()
         {
             Console.Write(a);
         }
         public User(string a, string b): this("welcome")
         {
             Console.Write(a + " " + b);
         }
     }
     class Program
     {
        static void Main(string[] args)
        {
            User user1 = new User(" to", "tutlane");
            Console.WriteLine();
            Console.WriteLine("\nPress Enter Key to Exit..");
            Console.ReadLine();
        }
     }
}

If you observe the above example, we created different constructors with different parameters, and we are calling one constructor from another constructor using this keyword.

 

When you execute the above c# program, you will get the result below.

 

C# Constructor Chaining Example Result

 

If you observe the above result, we call one constructor from another to achieve constructor chaining in the c# programming language.

 

This is how we can achieve constructor chaining in our applications using the c# programming language.