Monday, January 3, 2011

Constructors in C#

A constructor is a special method or function used to initialize & create the object of a class.

Types Of Constructory:
Constructor can be of two types
1. Class Constructor (Static Constructors)
2. Instance Constructor (Non Static Constructors)


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

Static constructors have the following properties:
1. A static constructor does not take access modifiers or have parameters.
2. A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
3. A static constructor cannot be called directly.
4.The user has no control on when the static constructor is executed in the program.
5. 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.
6. Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.


2. Instance Constructor (Non-static constructor): These constructors are normally used to initialize the mermbers and to create the constructor of class.

1. Public
A class constructor is by default public.

2. Private
When we create a private constructor, we cannot create object of the class directly from a client. Also we cannot inherit this class to another class.So by this way we can add security to class that it cannot be instantiated. Also a common use for a private constructor is to implement the singleton pattern.

3. Protected
When we declare a constructor protected then only drive class can create instance of that class.


Copy Constructor:
A copy constructor is a special constructor used to create a new object as a copy of an existing object. This constructor takes a single argument: a reference to the object to be copied. Unlike some languages, C# does not provide a copy constructor. If you create a new object and want to copy the values from an existing object, you have to write the appropriate method yourself.


class Person
{
private string name;
private int age;

// Copy constructor.
public Person(Person previousPerson)
{
name = previousPerson.name;
age = previousPerson.age;
}

// Instance constructor.
public Person(string name, int age)
{
this.name = name;
this.age = age;
}

// Get accessor.
public string Details
{
get
{
return name + " is " + age.ToString();
}
}
}

class TestPerson
{
static void Main()
{
// Create a new person object.
Person person1 = new Person("George", 40);

// Create another new object, copying person1.
Person person2 = new Person(person1);
System.Console.WriteLine(person2.Details);
}
}


Shallow Copy Vs Deep Copy:
Shallow copy copies the structure of the object, deep copy copies structure as well as data.

Dataset.Clone()- Shallow Copy
Dataset.Copy() -- Deep Copy


No comments:

Post a Comment