Saturday, January 12, 2013

Public, Private, Static, Parameterize and Copy Constructor in C#?


Private Constructor- 

Private Constructor is used to restrict the user to create an object outside the class i.e. No one can create the object of the class which contains private constructor. It is mostly used to make a Singleton class. 

Example

public class LoanData
{
    //Private Constructor 
    private LoanData()
    {
    }

    public static LoanData GetLoanData
    {
        get
        {
            object objData = HttpContext.Current.Session["LoanData"];
            if (objData == null || !(objData is LoanData))
            {
                //singleton object of LoanData 
                objData = new LoanData();
                HttpContext.Current.Session["LoanData"] = objData;
            }

            LoanData loanData = objData as LoanData;
            return loanData;
        }
    }
}

Public Constructor-

A Public constructor is a constructor which is called when a new object of a class is created. It is generally used to initialize the data. 

Characteristic 


  • Constructor does not have return type. 
  • Constructor can be parameterized.
  • Always Constructor name will be the same class name.
  • Constructor can be private and shared. 


Example

public class Employee
{
    //Public constructor.
    public Employee()
    {
    }
}

public class DisplayEmployee
{
    static void Main()
    {
        //Create a new Employee class object.
        Employee employee1 = new Employee();
    }
}

Static Constructor -

A static constructor is automatically called when first instance of the class is created. For second or more instances, it is not called. Access modifiers are not allowed on static constructor. A static constructor can not be called directly A static constructor initialized only static fields, it doesn't initialize non-static fields.

Example

public class Employee
{
    private string _name;
    private string _sal;
    static string _department;

    //Static constructor.
    static Employee()
    {
        _department = "Software";
    }
}

Copy constructor - 

Copy constructor is one type of constructor. It creates an object by coping value from another object. 
C# does not provide a copy constructor. If you want to create an object of a class and want to copy value from the existing object, you have to write the appropriate method for that.

Example

The Employee class contains a constructor which takes Employee class object as a parameter and assign the value from the parameter object into another new object.
     
public class Employee
{
    // Copy constructor.
    public Employee(Employee employeeObj)
    {
        _name = employeeObj._name;
        _sal = employeeObj._sal;
    }
}
   

Parameterize constructor - 

A Parameterize constructor is a constructor which contains one or more parameter. It is generally used when user needs to pass the value at the time of object creation.

Example

public class Employee
{
    private string _name;
    private string _sal;

    // constructor.
    public Employee()
    {
    }

    // Parameterize constructor.
    public Employee(string name, int sal)
    {
        this._name = name;
        this._sal = sal;
    }
}

public class DisplayEmployee
{
    static void Main()
    {
        //Create a new Employee class object.
        //Passing data at the time of object creation.
        Employee employee1 = new Employee("Anjali", 4000);
    }
}

No comments:

Post a Comment