Constructor :
1.Constructor is a special method which will invoke automatically whenever an object of class is cretaed.
2.constructor does not have return type and name of the constructor should be same as class Name.
3.if you create class with out constructor,the complier will create a default constructor for that class.
4.Every class can contain atleast one constructor.
5.constructor is responsible for initialization of object and allocating memory to class.(initialization of fields)
Note:
A class contain any number of constructors.
A constructor doesn’t have any return type even void.
A static constructor can not be a parameterized constructor.
Within a class you can create only one static constructor.
Types of Constructors:
1.Default Constructor
A Constructor with out any parameters is called default constructor.
2.Parameterized constructor
A constructor with atleast one parameter is called paramerterised constructor.
3.Copy constructor
A Parameterized constructor that contains a parameter of same class type is called as copy .
Constructor.
4.Static constructor
Static constructor will not accept any parameters becaues it is automatically called by CLR.
Static constructor will not have any access modifiers
Only one Static constructor will allowed.
Static constructor will execute automatically whenever we create first instance of class .
5.private constructor
Private constructor is used to restrict the class from being instantiated when it contains every member as static.
One use of private construct is when we have only static member.
Once we provide a constructor that is either private or public or any, the compiler will not allow us to add public constructor without parameters to the class.
If we want to create object of class even if we have private constructors then we need to have public constructor along with private constructor.
Example :
public class Sample
{
public string Param1 { get; set; }
public string Param2 { get; set; }
public static string test { get; set; }
//default constructor
public Sample()
{
Param1 = "Hello";
Param2 = "Constructor";
}
//parameterized constructor
public Sample(string str1, string str2)
{
Param1 = str1;
Param2 = str2;
}
//copy constructor
public Sample(Sample obj)
{
Param1 = obj.Param1;
Param2 = obj.Param2;
}
// static constructor
static Sample()
{
test = "Static Constructor";
}
//private constructor
private Sample()
{
}
}
1.Constructor is a special method which will invoke automatically whenever an object of class is cretaed.
2.constructor does not have return type and name of the constructor should be same as class Name.
3.if you create class with out constructor,the complier will create a default constructor for that class.
4.Every class can contain atleast one constructor.
5.constructor is responsible for initialization of object and allocating memory to class.(initialization of fields)
Note:
A class contain any number of constructors.
A constructor doesn’t have any return type even void.
A static constructor can not be a parameterized constructor.
Within a class you can create only one static constructor.
Types of Constructors:
1.Default Constructor
A Constructor with out any parameters is called default constructor.
2.Parameterized constructor
A constructor with atleast one parameter is called paramerterised constructor.
3.Copy constructor
A Parameterized constructor that contains a parameter of same class type is called as copy .
Constructor.
4.Static constructor
Static constructor will not accept any parameters becaues it is automatically called by CLR.
Static constructor will not have any access modifiers
Only one Static constructor will allowed.
Static constructor will execute automatically whenever we create first instance of class .
5.private constructor
Private constructor is used to restrict the class from being instantiated when it contains every member as static.
One use of private construct is when we have only static member.
Once we provide a constructor that is either private or public or any, the compiler will not allow us to add public constructor without parameters to the class.
If we want to create object of class even if we have private constructors then we need to have public constructor along with private constructor.
Example :
public class Sample
{
public string Param1 { get; set; }
public string Param2 { get; set; }
public static string test { get; set; }
//default constructor
public Sample()
{
Param1 = "Hello";
Param2 = "Constructor";
}
//parameterized constructor
public Sample(string str1, string str2)
{
Param1 = str1;
Param2 = str2;
}
//copy constructor
public Sample(Sample obj)
{
Param1 = obj.Param1;
Param2 = obj.Param2;
}
// static constructor
static Sample()
{
test = "Static Constructor";
}
//private constructor
private Sample()
{
}
}