Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Constructor and its types in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 357
    Comment on it

    Constructors in c#

    Constructor is a special method of a class that gets called when an object of a class is created. They are mainly used for initialization and memory allocation for member variables of class. There will always be a default constructor of a class even if you don’t create it. There is always at least one constructor in a class.

    Constructors do not have any written types. There may be n number of constructors in a class. A constructor`s name should match the name of its class.  Here is an example

    public class MyConstructor
        {
            public MyConstructor()
            {
                Console.WriteLine("My Constructor is called");
            }
        }
    

    Types of constructors

    There are five types of Constructors which are as follows

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

     

    Default Constructor- Constructor which does not have any parameter is called default constructor. It is used for initializing member variable with default values. Here is an example

    class Program
        {
            static void Main(string[] args)
            {
                MyConstructor myConstructor = new MyConstructor();
                Console.WriteLine(myConstructor.name);
                Console.Read();
            }
        }
    
        public class MyConstructor
        {
            public string name = string.Empty;
            public MyConstructor()
            {
                name = "Abhishek";
            }
        }
    

    Output

    Abhishek

    Parameterized Constructor-A constructor which has at least one parameter is called parameterized constructor. It can initialize the member variables with the parameter passed to it. Here is an example.

    class Program
        {
            static void Main(string[] args)
            {
                MyConstructor myConstructor = new MyConstructor("Abhishek");
                Console.WriteLine(myConstructor.name);
                Console.Read();
            }
        }
    
        public class MyConstructor
        {
            public string name = string.Empty;
            public MyConstructor( string empName)
            {
                name = empName;
            }
        }
    

     

    Output

     

    Abhishek

    Copy Constructor- When a parameterized constructor contains the parameter of same class type is called Copy Constructor. It is used for initializing new instance of a call to the values of an existing instance. Here is an example

    class Program
        {
            static void Main(string[] args)
            {
                MyConstructor myConstructor = new MyConstructor("Abhishek");
                MyConstructor mySecondConstructor = new MyConstructor(myConstructor);
                Console.WriteLine(mySecondConstructor.name);
                Console.Read();
            }
        }
    
        public class MyConstructor
        {
            public string name = string.Empty;
            
            public MyConstructor( string empName)
            {
                name = empName;
            }
    
            public MyConstructor(MyConstructor myConstructor)
            {
                name = myConstructor.name;
            }
        }
    

     

    Output

     

    Abhishek

    Static Constructor- Static constructors are used to initialize the static members of a class. They are invoked only once and that is when the first time an instance of a class is created. Code writer inside static constructor will only be executed single time. Here is an example

    class Program
        {
            static void Main(string[] args)
            {
                MyConstructor myConstructor = new MyConstructor();
                MyConstructor mySecondConstructor = new MyConstructor();            
                Console.Read();
            }
        }
    
        public class MyConstructor
        {        
            static MyConstructor()
            {
                Console.WriteLine("Static constructor called");
            }
    
            public MyConstructor()
            {
                Console.WriteLine("Default constructor called");
            }
        }
    

     

    Output

     

    Static constructor called

    Default constructor called

    Default constructor called

    Private Constructor- Private constructors are used in classes which only have static members.  If a class has all private constructors and no public constructor, then you cannot create an object of that class. Once we declare a private constructor, we cannot add any other public or private constructor without parameter. Here is an example.

    public class MyConstructor
        {        
            private MyConstructor()
            {
                Console.WriteLine("Static constructor called");
            }
           
        }
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: