Monday, January 14, 2019

Types of Constructor in C#

Constructor -> Constructor is special kind of methods used for initialization of a class.

Types of Constructors
                Default – Without having any parameter
                public string fName;
public clsConnect()
        {
            fName = "First Name";
        }
                Parameterized – Constructor at least one parameter
                public string fName;
public clsConnect(string sFName)
        {
            fName = sFName;
        }

                Copy – Parameterized constructor that contains a parameter of same class type. The main purpose of this type of constructor is to initialize the new instant to the value of the existing instance.
 public string fName;
public clsConnect(string sFName)
        {
            fName = sFName;
        }

public clsConnect(clsConnect objCon)
        {
            fName = objCon.fName;
        }
                Static–used to initialize the static data members of the class. It only called once while creation of the first instance of the class.
static string fName;
static clsConnect()
        {
            fName = "First Name";
        }
                Private – You cannot create the instance of private constructor class.
private clsConnect()
        {

        }

No comments: