Constructors And Its Type In C#

In this article, we are going to learn what is constructors and their type in C#.

What is Constructor?

A Constructor is a method that is called automatically whenever an instance of that class is created. It is mainly used for initializing the private field of the class. the construction should have the same name as class.

Example
class TheCodeHubs
{
   public TheCodeHubs() { }
}

Point to remember for a constructor.

  • A class can have any number of constructors but you can create only one static constructor.
  • A constructor doesn’t have any return type, not even void.
  • A static constructor can not be a parametrized constructor.
  • A static constructor cannot be a parameterized constructor.

Types of Constructor

  1. Default Constructor.
  2. Parameterized Constructor.
  3. Copy Constructor.
  4. Static Constructor.
  5. Private Constructor.

Default Constructor

A constructor with no parameter is known as the default constructor. It is called every time when an object of a class is created.

using System;  
public class Student  
{  
   public Student()  
   {  
       Console.WriteLine("Default constructor is called");  
   }  
   public static void Main(string[] args)  
   {  
       Student stud1 = new Student();  
       Student stud2 = new Student();  
   }  
}
Output
Default constructor is called
Default constructor is called

Parameterized Constructor

A constructor that has one or more parameters is known as parameterized constructor.

public class Test  
{  
  public int a;
  public int b;
  public Test(int x,int y)  
  {  
      a = x;
      b = y;
  }  
  public void addition()
  {
     Console.WriteLine("addition is",a+b);
   }
}  
class TestDemo	
{
   public static void Main(string[] args)  
   {  
     Test test = new Test(10,10);  
     test.addition();  
   }  
}
Output
addition is 20

Copy Constructor

The constructor which creates an object by copying variables from another object is called a copy constructor.

class Student
{
    private string name;
    private int marks;
    // declaring Copy constructor.
    public Student(Student stud)   
    {
        name = stud.name;
        marks = stud.marks;
    }
    // Instance constructor.
    public Student(string name, int marks)  
    {
       this.name = name;
       this.marks = marks;
    }
   // Get details of Student
   public string Details     
   {
      get
      {
          return  " The marks of " + name +" is "+ marks.ToString();
      }
   }
}
class StudentDetail
{
     static void Main()
     {
        Student stud1 = new Student("Riya", 93);  // Create a new Student object.
        Student stud2 = new Student(stud1);     //  stud1 details is copied to stud2.
        Console.WriteLine(stud2.Details);
     }
}
Output
The marks of Riya is 93

Static Constructor

Static Constructor has to be called only once in the class and it has been called during the creation of the first reference to a static member in the class. A static constructor initialized static fields or data of the class.

  • It can’t be called directly.
  • It does not contain any access modifiers or any parameters.
  • It is called automatically to initialize the class before the first instance is created.
using System;
namespace staticConstructor {

class Student {
  static Student()
  {
    Console.WriteLine("Static Constructor Called");
  }
  public static void Main()
  {
    Student obj = new Student();
       }
   }
}
Output
Static Constructor Called

Private Constructor

If a constructor declares as private then It is not possible for other classes to derive from this class and also it’s not possible to create an instance of this class.

using System;
namespace privateConstructor 
{
    public class Test {
    private Test()
    {
    }
 
    // declare static variable field
    public static int count;
 
    // declare static method
    public static int Count()
    {
        return ++count;
    }
 
    // Main Method
    public static void Main()
    {
       Test.count = 11;
       Test.Count();
       Console.WriteLine(Test.count);
       Test.Count();
       Console.WriteLine(Test.count);
    }
 }
}
Output
12
13

That’s it.

Also check, Abstract Class Vs Interface In C#

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories