1. What is Constructor.
A special method of class that will automatically called when instance of class is created.
Points to remember-
1. Constructor will not have any return type not even void.
2. Constructor Name is same name as class name.
3. it is used for allocation of memory.
4. it is by default public.
5. by default c# will create default constructor internally.
6. we can create private constructor.
7. Static constructor can not be parametrized constructor.
8. Within a class you can create only one static constructor.
Five Types of Constructor:-
1. Default Constructor
2. Parametrized
3.Copy Constructor
4. Static constructor
5. Private constructor
1. Default Constructor:- A constructor without any parameter is called default constructor.
Drawback: Every instance of the class will be initialised at same value.
it is not possible to intialize each instance of the class to different values.
All numeric field of the class initialise zero. and string null.
Note: If the class is abstract, then the accessibility of the default constructor is protected. Otherwise, the accessibility for the default constructor is public.
Example:-
public class SampleA {
public int a;
public string strParam;
public SampleA() {
a = 10;
strParam = "This is Example of Default Constructor";
}
}
2. Parametrized constructor:
A constructor with at least one parameter is called a parametrized constructor.
The advantage of a parametrized constructor is that you can initialize each instance of the class to different values.
public int a, b;
public paraconstrctor(int x, int y) // decalaring Paremetrized Constructor with ing x,y parameter
{
a = x;
b = y;
}
3. Copy Constructor:-
A Constructor which creates an object copy by another object is call copy constructor.
copy constructor is to initialize a new instance to the values of an existing instance.
Example:-
class Program
{
static void Main(string[] args)
{
SampleA a = new SampleA(5,"Alok");
SampleA objb = new SampleA(a);
Console.ReadKey();
}
}
public class SampleA {
public int a;
public string strParam;
public SampleA(SampleA ObjA) {
a = ObjA.a;
strParam = ObjA.strParam;
Console.WriteLine(a + "str" + strParam);
}
public SampleA(int z,string strname){
a=z;
strParam = strname;
Console.WriteLine(z + "str" + strParam);
}
4. Private Constructor:
When a constructor is created with a private specifier, it is not possible for other classes to derive from this class,
neither is it possible to create an instance of this class.
They are usually used in classes that contain static members only.
Some key points of a private constructor are:
One use of a private constructor is when we have only static members.
It provides an implementation of a singleton class pattern
Once we provide a constructor that is either private or public or any, the compiler will not add the parameter-less public constructor to the class.
Example:
class Program
{
static void Main(string[] args)
{
//SampleA obj = new SampleA(); can not Create instance
SampleA.visitcount(5000);
Console.ReadKey();
}
}
public class SampleA {
public int a;
public string strParam;
private SampleA() {
a++;
Console.WriteLine(a);
}
public static int counter;
public static void visitcount(int counter)
{
counter++;
Console.WriteLine(counter);
}
5. What is Static Constructor:-
When a constructor is created as static, it will be invoked only once for all of instances of the class and
it is invoked during the creation of the first instance of the class or the first reference to a static member in the class.
Some key points of a static constructor is:
Example:-
class Program
{
static void Main(string[] args)
{
SampleA.Employee();
SampleA.Salary();
//
SampleA.Employee();
Console.ReadKey();
}
}
public class SampleA
{
static SampleA()
{
}
public static void Employee()
{
Console.WriteLine("This is Example of Static Constructor");
}
public static void Salary() {
Console.WriteLine("Salary");
}
}
A special method of class that will automatically called when instance of class is created.
Points to remember-
1. Constructor will not have any return type not even void.
2. Constructor Name is same name as class name.
3. it is used for allocation of memory.
4. it is by default public.
5. by default c# will create default constructor internally.
6. we can create private constructor.
7. Static constructor can not be parametrized constructor.
8. Within a class you can create only one static constructor.
Five Types of Constructor:-
1. Default Constructor
2. Parametrized
3.Copy Constructor
4. Static constructor
5. Private constructor
1. Default Constructor:- A constructor without any parameter is called default constructor.
Drawback: Every instance of the class will be initialised at same value.
it is not possible to intialize each instance of the class to different values.
All numeric field of the class initialise zero. and string null.
Note: If the class is abstract, then the accessibility of the default constructor is protected. Otherwise, the accessibility for the default constructor is public.
Example:-
public class SampleA {
public int a;
public string strParam;
public SampleA() {
a = 10;
strParam = "This is Example of Default Constructor";
}
}
2. Parametrized constructor:
A constructor with at least one parameter is called a parametrized constructor.
The advantage of a parametrized constructor is that you can initialize each instance of the class to different values.
public int a, b;
public paraconstrctor(int x, int y) // decalaring Paremetrized Constructor with ing x,y parameter
{
a = x;
b = y;
}
3. Copy Constructor:-
A Constructor which creates an object copy by another object is call copy constructor.
copy constructor is to initialize a new instance to the values of an existing instance.
Example:-
class Program
{
static void Main(string[] args)
{
SampleA a = new SampleA(5,"Alok");
SampleA objb = new SampleA(a);
Console.ReadKey();
}
}
public class SampleA {
public int a;
public string strParam;
public SampleA(SampleA ObjA) {
a = ObjA.a;
strParam = ObjA.strParam;
Console.WriteLine(a + "str" + strParam);
}
public SampleA(int z,string strname){
a=z;
strParam = strname;
Console.WriteLine(z + "str" + strParam);
}
4. Private Constructor:
When a constructor is created with a private specifier, it is not possible for other classes to derive from this class,
neither is it possible to create an instance of this class.
They are usually used in classes that contain static members only.
Some key points of a private constructor are:
One use of a private constructor is when we have only static members.
It provides an implementation of a singleton class pattern
Once we provide a constructor that is either private or public or any, the compiler will not add the parameter-less public constructor to the class.
Example:
class Program
{
static void Main(string[] args)
{
//SampleA obj = new SampleA(); can not Create instance
SampleA.visitcount(5000);
Console.ReadKey();
}
}
public class SampleA {
public int a;
public string strParam;
private SampleA() {
a++;
Console.WriteLine(a);
}
public static int counter;
public static void visitcount(int counter)
{
counter++;
Console.WriteLine(counter);
}
5. What is Static Constructor:-
When a constructor is created as static, it will be invoked only once for all of instances of the class and
it is invoked during the creation of the first instance of the class or the first reference to a static member in the class.
Some key points of a static constructor is:
- A static constructor does not take access modifiers or have parameters.
- A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
- A static constructor cannot be called directly.
- The user has no control on when the static constructor is executed in the program.
- 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.
Example:-
class Program
{
static void Main(string[] args)
{
SampleA.Employee();
SampleA.Salary();
//
SampleA.Employee();
Console.ReadKey();
}
}
public class SampleA
{
static SampleA()
{
}
public static void Employee()
{
Console.WriteLine("This is Example of Static Constructor");
}
public static void Salary() {
Console.WriteLine("Salary");
}
}
No comments:
Post a Comment