C# Constructors with Examples

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

 

In case, if we create a class without having any constructor, then the compiler will automatically create a one default constructor for that class. So, there is always one constructor will exist in every class.

 

In c#, a class can contain more than one constructor with different type of arguments and 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 class.

C# Constructor Syntax

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

 

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

 

public class User

{

// Constructor

public User()

{

// Your Custom Code

}

}

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

C# Constructor Types

In c#, we have a different type of constructors available, those are

 

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

C# Default Constructor

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

 

Following is the example of defining the default constructor in 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 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 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), then automatically our constructor method will be called.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Default Constructor Example Result

 

If you observe 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, then we will call it as parameterized constructor and the every instance of class will be initialized with parameter values.

 

Following is the example of defining the parameterized constructor in 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 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 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 required parameters, then automatically our constructor method will be called.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Parameterized Constructor Example Result

 

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

 

In next chapters, we will learn about copy constructor, static constructor and private constructor in c# programming language with examples.

C# Constructor Overloading

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

 

Following is the example of implementing a constructor overloading in 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 above example, we created a class called “User” and overloaded a constructor “User()” by creating another constructor “User(string, string)” with same name but with different parameters.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Constructor Overloading Example Result

 

If you observe above result, the respective constructor methods will be called 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 above example, we created a different constructors with different parameters and we are calling one constructor from another constructor using this keyword.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Constructor Chaining Example Result

 

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

 

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

Topics Covered