Loading, please wait...

A to Z Full Forms and Acronyms

Different types of constructors in C# programming

Sep 06, 2019 C#, Constructors in C#, Constructors, 7847 Views
This article will explain you different types of constructors in C# programming.

What is constructor?

A constructor is a special method that is used to initialize an object. A constructor is invoked at the time of object creation. Constructor name must be the same as its class name. A constructor must have no explicit return type.

What are the types of constructors in c#?

There are five different types of constructors

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

Different between constructors and method

Constructor

Method

The constructor is used to initialize an object.

The method is used to expose the behavior of an object.

The constructor cannot have a return type.

The method can or cannot have a return type.

A constructor must be the same as the class name.

Method name cannot be same as the class name.

A constructor is invoked implicitly.

The method is invoked explicitly.

 

What is the default constructor in c#?

A constructor without any parameter is called the default constructor. If we do not create constructor the class will automatically call default constructor when the object is created.

using System;
 
namespace IntroductionToCunstructor
{
    public class Customer 
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
 
        public Customer()
        {
 
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            var customer = new Customer  ();
            customer.Id = 1;
            customer.FirstName = "Farhan";
            customer.LastName = "Ahmed";
 
            Console.WriteLine("Customer Id:{0}",customer.Id);
            Console.WriteLine("First Name:{0}",customer.FirstName);
            Console.WriteLine("Last Name:{0}",customer.LastName);
            Console.WriteLine("Full Name:"+ customer.FirstName + " " +customer.LastName);
 
            Console.ReadLine();
        }
    }
}

 

What is the parameterized constructor in c#?

A constructor with at least one parameter is called the parameterized constructor.

using System;
 
namespace IntroductionToCunstructor
{
    public class Customer 
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
 
        public Customer(int id, string firstName, string lastName)
        {
            Id = id;
            FirstName = firstName;
            LastName = lastName;
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            var customer = new Customer(1,"Farhan","Ahmed");
 
            Console.WriteLine("Customer Id:{0}",customer.Id);
            Console.WriteLine("First Name:{0}",customer.FirstName);
            Console.WriteLine("Last Name:{0}",customer.LastName);
            Console.WriteLine("Full Name:"+ customer.FirstName + " " +customer.LastName);
 
            Console.ReadLine();
        }
    }
}

What is copy constructor in c#?

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

using System;
 
namespace IntroductionToCunstructor
{
    public class Customer
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
 
        //copy constructor
        public Customer(Customer customer)
        {
            Id = customer.Id;
            FirstName = customer.FirstName;
            LastName = customer.LastName;
        }
 
        public Customer()
        {
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            var customer = new Customer();
            customer.Id = 1;
            customer.FirstName = "Farhan";
            customer.LastName = "Ahmed";
 
            Console.WriteLine("Customer Id:{0}",customer.Id);
            Console.WriteLine("First Name:{0}",customer.FirstName);
            Console.WriteLine("Last Name:{0}",customer.LastName);
            Console.WriteLine("Full Name:"+ customer.FirstName + " " +customer.LastName);
 
            Console.ReadLine();
        }
    }
}

 

What is static constructor in c#?

A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.

 

Characteristic of static constructor

  1. A static constructor does not take access modifiers or have parameters.
  2. A class or struct can only have one static constructor.
  3. Static constructors cannot be inherited or overloaded.
  4. A static constructor cannot be called directly and is only meant to be called by the common language runtime (CLR). It is invoked automatically.
  5. The user has no control on when the static constructor is executed in the program.

 

Usage of static constructor

  • A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
  • Static constructors are also useful when creating wrapper classes for unmanaged code when the constructor can call the LoadLibrary
  • Static constructors are also a convenient place to enforce run-time checks on the type parameter that cannot be checked at compile-time via constraints (Type parameter constraints).
using System;
 
namespace IntroductionToCunstructor
{
    public class Customer
    {
        public static string FistName;
        public static string LastName;
 
        static Customer()
        {
            Console.WriteLine("I am Platinum Customer");
        }
 
        public Customer()
        {
            Console.WriteLine("I am Regular Customer");
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            var customer = new Customer();
            var customr1 = new Customer();
 
            Console.ReadLine();
        }
    }
}

What is private constructor in c#?

A private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class.

 

Usage of private constructor

The use of private constructor is to serve singleton classes. A singleton class is one which limits the number of objects created to one. Using private constructor we can ensure that no more than one object can be created at a time

  1. One use of private constructor is when we have an only static member.
  2. It provides an implementation of singleton class pattern
  3. Once we provide constructor (private/public/any) the compiler will not add the no parameter public constructor to any class.

 

using System;
 
namespace IntroductionToCunstructor
{
    public class Customer
    {
        private Customer()
        {
 
        }
        public static int CustomerCount;
        public static int IncreaseCustomerCount()
        {
            return ++CustomerCount;
        }
 
 
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            //var customer = new Customer();
 
            Customer.CustomerCount = 1000;
            Customer.IncreaseCustomerCount();
 
            Console.WriteLine("Present Customer Count:{0}", Customer.CustomerCount);
            Console.ReadLine();
        }
    }
}
A to Z Full Forms and Acronyms