=> A special method of the class that will be automatically invoked. When an instance of the class is created.
Constructor can be classified into five types.
1. Default Constructor.
2. Parametrized Constructor.
3. Copy Constructor.
4. Static Constructor.
5. Private Constructor.
1. Default Constructor :-
A constructor without any parameter is called as default constructor. Drawback of default constructor is every instance of the class will be initialized to same values.
2. Parametrized Constructor :-
A constructor with at least one parameter is called as parametrized constructor. Advantage of parametrized constructor is we can initialize each instance of the class to different values.
Example for parametrized constructor
using System;
namespace Praveen
{
class Test1
{
//Private fields of class
int A, B;
// Default constructor
public Test1()
{
A = 10;
B = 20;
}
// Parametrized constructor
public Test1(int X, int Y)
{
A = X;
B = Y;
}
// Method to print
public void print()
{
Console.WriteLine("A={0}\t B={1}", A, B);
}
class MainClass
{
static void Main(string[] args)A
{
Test1 T1 = new Test1(); //Default
Test1 T2 = new Test1(80, 40); //Parametrized
T1.print();
T2.print();
Console.Read();
}
}
}
}
OutPut
A = 10 B = 20
A = 80 B = 40
3. Copy Constructor :-
A parametrized constructor that contains a parameter of same class type is called as copy constructor. Main purpose of copy constructor is initialize new instance to the value of an existing instance.
Example of copy constructor
using System;
namespace Praveen
{
class Test2
{
int A, B;
public Test2()
{
A = 10;
B = 20;
}
// Parametrized constructor
public Test2(int X, int Y)
{
A = X;
B = Y;
}
// Copy constructor
public Test2(Test2 T)
{
A = T.A;
B = T.B;
}
public void print()
{
Console.WriteLine("A={0}\t B={1}", A, B);
}
class CopyConstructor
{
static void Main( )
{
Test2 T2 = new Test2(80,90);
// Invoking copy constructor
Test2 T3 = new Test2(T2);
T2.print();
T3.print();
Console.Read();
}
}
}
}
OutPut
A = 80 B = 90
A = 80 B = 90
Example of static constructor
using System;
namespace Praveen
{
class Test3
{
public Test3()
{
Console.WriteLine("Instance Constructor");
}
static Test3()
{
Console.WriteLine("Static Constructor");
}
}
class StaticConstructor
{
static void Main()
{
//Static Constructor and instance constructor, both are invoked for first instance.
Test3 T1 = new Test3();
//Only instance constructor is invoked.
Test3 T2 = new Test3();
Console.Read();
}
}
}
OutPut
Static Constructor
Instance Constructor
Instance Constructor
5. Private Constructor :-
A private constructor is with private access modifier is known as private constructor.
Private constructor is used in classes that contain static member only.
Some unique points related to constructors are as follows
A class can have 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.
Constructor can be classified into five types.
1. Default Constructor.
2. Parametrized Constructor.
3. Copy Constructor.
4. Static Constructor.
5. Private Constructor.
1. Default Constructor :-
A constructor without any parameter is called as default constructor. Drawback of default constructor is every instance of the class will be initialized to same values.
2. Parametrized Constructor :-
A constructor with at least one parameter is called as parametrized constructor. Advantage of parametrized constructor is we can initialize each instance of the class to different values.
Example for parametrized constructor
using System;
namespace Praveen
{
class Test1
{
//Private fields of class
int A, B;
// Default constructor
public Test1()
{
A = 10;
B = 20;
}
// Parametrized constructor
public Test1(int X, int Y)
{
A = X;
B = Y;
}
// Method to print
public void print()
{
Console.WriteLine("A={0}\t B={1}", A, B);
}
class MainClass
{
static void Main(string[] args)A
{
Test1 T1 = new Test1(); //Default
Test1 T2 = new Test1(80, 40); //Parametrized
T1.print();
T2.print();
Console.Read();
}
}
}
}
OutPut
A = 10 B = 20
A = 80 B = 40
3. Copy Constructor :-
A parametrized constructor that contains a parameter of same class type is called as copy constructor. Main purpose of copy constructor is initialize new instance to the value of an existing instance.
Example of copy constructor
using System;
namespace Praveen
{
class Test2
{
int A, B;
public Test2()
{
A = 10;
B = 20;
}
// Parametrized constructor
public Test2(int X, int Y)
{
A = X;
B = Y;
}
// Copy constructor
public Test2(Test2 T)
{
A = T.A;
B = T.B;
}
public void print()
{
Console.WriteLine("A={0}\t B={1}", A, B);
}
class CopyConstructor
{
static void Main( )
{
Test2 T2 = new Test2(80,90);
// Invoking copy constructor
Test2 T3 = new Test2(T2);
T2.print();
T3.print();
Console.Read();
}
}
}
}
OutPut
A = 80 B = 90
A = 80 B = 90
4. Static Constructor :-
You can create a constructor as
static and when a constructor is created as static, it will be invoked
only once for any number of instances of the class and it is during the
creation of first instance of the class or the first reference to a
static member in the class. Static constructor is used to initialize
static fields of the class and to write the code that needs to be
executed only once.Example of static constructor
using System;
namespace Praveen
{
class Test3
{
public Test3()
{
Console.WriteLine("Instance Constructor");
}
static Test3()
{
Console.WriteLine("Static Constructor");
}
}
class StaticConstructor
{
static void Main()
{
//Static Constructor and instance constructor, both are invoked for first instance.
Test3 T1 = new Test3();
//Only instance constructor is invoked.
Test3 T2 = new Test3();
Console.Read();
}
}
}
OutPut
Static Constructor
Instance Constructor
Instance Constructor
5. Private Constructor :-
A private constructor is with private access modifier is known as private constructor.
Private constructor is used in classes that contain static member only.
Some unique points related to constructors are as follows
A class can have 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.
0 comments:
Post a Comment