Constructor Methods in c#

Constructor Methods in c#

In the C# programming language, constructors are special, user-defined, variable-with-parameter class methods that are automatically called when a new instance or function parameter object is created in a custom class. In a C# program, constructor methods created in a class are used to declare and initialize data member fields of an object, or to create and programmatically call a class object.

Constructor Methods in c#

Special features of constructors in C# programming.

  • Constructor name – The name of a class parameter constructor created in the current class must always be the same as the class name.
  • No return type – A constructor method created in a class has no return data type, not even a void data type.
  • Automatic call – Constructors are automatically called when a new object is created in a user-defined class. This eliminates the need for the C# programmer to invoke them in an explicit order.
  • Overloading – A user-defined class can have multiple constructor methods or data types with multiple individual variable parameter lists (constructor overloading). This allows you to create multiple individual type class objects if needed.

Basic C# constructor syntax.

Here is an example of a basic constructor method in a class.

class Employee

{

public string Emp_Name;

public int Emp_Age;

// Here we define a constructor method

public Employee(string emp_name, int emp_age)

{

Emp_Name = emp_name;

Emp_Age = emp_age;

}

}

Here is a basic constructor method example.

  • Here, a constructor method Employee(string emp_name, int emp_age) is defined in the Employee class, which initializes the Emp_Name and Emp_Age fields of the existing Employee class when a new object is created.

Creating an object with a constructor in C#.

When C# programmers create a new object in a class, they can pass class parameter argument values ​​as needed. These are passed to the constructor to initialize the new object in the class.

class Employee

{

static void Main()

{

//Here we are creating a class object of the Employee class

Employee employees = new Employee(“Bhavishi Deora”, 21);

// Here we’re accessing the properties of the employee class object.

Console.WriteLine($”Emp_Name – {employees.Emp_Name}, Emp_Age – {employees.Emp_Age}”);

}

}

Default Constructor in C#.

If C# programmers don’t explicitly create or define a constructor in a program, C# programming provides a default constructor method. This doesn’t input any parameter values ​​by default. It simply initializes or assigns the fields of a user-defined class object to their default values. For example, you can define or assign a value of null for a reference type, 0 for a numeric data type, or True or False for a Boolean.

Example with a default constructor in C#.

class Employee

{

public string Emp_Name;

public int Emp_Age;

// Here we use the default class constructor, C# provides this if no constructor method is defined.

public Employee()

{

Emp_Name = “Random”;

Emp_Age = 0;

}

}

class Employee

{

static void Main()

{

// Here we use the default constructor method.

Employee employees = new Employee();

Console.WriteLine($”Emp_Name – {employees.Emp_Name}, Emp_Age – {employees.Emp_Age}”);

}

}

Default Constructor Explanation in C#.

  • In this program, the C# programmer has not created or defined any manual constructor. C# programming automatically provides a default constructor method function. Here in the constructor method Emp_Name is defined or set to “Random”, and Employee Age is set to 0 value according to Emp_Age constructor.

Constructor overloading concept in C#.

In C# programming, programmers can create multiple user-defined constructor objects in a class with multiple individual class parameter lists. This entire process is called constructor overloading.

Example of constructor overloading in C#.

class Employee

{

public string Emp_Name;

public int Emp_Age;

public string Location;

// Here we create the employee constructor with emp_name and emp_age with two parameters.

public Employee(string emp_name, int emp_age)

{

Emp_Name = emp_name;

Emp_Age = age;

Location = “Undefined”;

}

// here we Loaded constructor method with emp_name, emp_age, and location parameter

public Employee(string emp_name, int emp_age, string location)

{

Emp_Name = emp_name;

Emp_Age = emp_age;

Location = location;

}

}

class employee

{

static void Main()

{

// here we Using the employee class constructor method with two variable parameters

Employee employees = new Employee(“Siddhi Deora”, 19);

Console.WriteLine($”Emp_Name – {employees.Emp_Name}, Emp_Age – {employees.Emp_Age}, Location – {employees.location}”);

// Here we are using the method overloaded constructor with three employee class variable parameters.

Employee employees1 = new Employee(“Bhavishi Deora”, 21, “Abc Street New York”);

Console.WriteLine($”Emp_Name – {employees1.Emp_Name}, Emp_Age – {employees1.Emp_Age}, Location – {employees1.location}”);

}

}

Constructor method overloading explanation in C#.

  • Here, in this program, we have overloaded the constructor to allow two different methods or steps to initialize the Employee class object created with the parameters.
  • The first method can be used with only the employee name and employee age (using the first constructor).
  • The second method can be used with the Employee name, Employee age, and Employee location in the Employee class (using another constructor).

Constructor chaining concept in C#.

The constructor chaining method or concept in C# programming means calling a constructor method from another constructor method declared in the same class. You can do this by using the ‘this’ reserved keyword in a C# program. This allows you to reuse another constructor to initialize a user-defined constructor method, and this method helps you avoid program source code duplication.

Example of constructor chaining in C#.

class Employee

{

public string Emp_Name;

public int Emp_Age;

public string Location;

// here we create Constructor with emp_name and emp_age parameter

public Employee(string emp_name, int emp_age)

{

Emp_Name = emp_name;

Emp_Age = emp_age;

// here it Calls another constructor with an additional location parameter element

Location=”Undefined”;

}

// here we define a Constructor with emp_name, emp_age, and location default parameter

public Employee(string emp_name, int emp_age, string location) : this(emp_name, emp_age)

{

Location = location;

}

}

Here in this constructor chaining example.

  • Here in this program, the second constructor method calls the first constructor method using the this(emp_name, emp_age) method to initialize Emp_Name and Emp_Age, and then assigns the Employee Location field values.

Constructor Methods Summary in C#.

  • Constructor Method – In C# programming, constructors are special user-defined functions or class methods. They are used in C# programs to initialize or assign new class object parameter variable values.
  • Default Constructor – If the C# programmer has not defined any constructor method in the current program, the C# program provides you with a default constructor method. It initializes the existing class object variable parameter fields to their default values.
  • Parameterized Constructors – These allow you to pass values ​​when creating a new object in a class, and use those values ​​to initialize user-defined class constructor method fields.
  • Constructor Overloading – C# programmers can define multiple constructors with different variable parameter lists to create new objects from multiple individual methods.
  • Constructor chaining – This process allows you to call a constructor method parameter value from another class method defined in the same class.

Leave a Reply