Constructors with examples in C#.NET
A special method of the class that will be automatically invoked when an instance of the class is created is called as constructor. So we can say a constructor is a public method, which is automatically invoked when an object is created.
Now we will see some of the peculiarities of constructors
- Constructors have the same name as that of the class name
- It has no return type not even void
- Constructor must be declared with the keyword 'public'
- Constructors can be overloaded , that means we can pass parameters along with a constructor.
Constructors can be classified into 5 types
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Static Constructor
5. Private Constructor
Default Constructor : A constructor without any parameters is called as default constructor. Drawback of default constructor is every instance of the class will be initialized to same values and it is not possible to initialize each instance of the class to different values.
Example for Default Constructor
using System;
//Example Class to check whether number is even or not
class Even{
private int no;
//Default Constructor
public Even()
{
this.no= 2;
}
public void check()
{
if (n % 2 ==0)
Console.WriteLine(n +" is even number");
else
Console.WriteLine(n +" is odd number");
}
}
class MyMain
{
public static void Main()
{
//Calling Default Constructor
Even evobj= new Even();
evobj.check();
Console.Read();
}
}
Parameterized Constructor : A constructor with at least one parameter is called as parameterized constructor. Advantage of parameterized constructor is you can initialize each instance of the class to different values.
Example for Parameterized Constructor
using System;
//Example Class to check whether number is even or not
class Even
{
private int no;
//Default Constructor
public Even()
{
this.no= 2;
}
//Paramerized Constructor
public Even(int no)
{
//A Constructor is used to initilize private fields of a class
this.no= no;
}
public void check()
{
if (n % 2 ==0)
Console.WriteLine(n +" is even number");
else
Console.WriteLine(n +" is odd number");
}
}
//Using Class in the program
class MyMain
{
public static void Main()
{
//Calling Parametrized Constructor
Even evobj= new Even(5);
evobj.check();
Console.Read();
}
}
Copy Constructor : A parameterized constructor that contains a parameter of same class type is called as copy constructor. Main purpose of copy constructor is to initialize new instance to the values of an existing instance.
Example for Copy Constructor
using System;
class Test
{
int A, B;
public Test(int X, int Y)
{
A = X;
B = Y;
}
//Copy Constructor
public Test(Test T)
{
A = T.A;
B = T.B;
}
public void Print()
{
Console.WriteLine("A = {0}\tB = {1}", A, B);
}
}
class CopyConstructor
{
static void Main()
{
Test T1 = new Test(80, 90);
//Invoking copy constructor
Test T2 = new Test(T1);
T1.Print();
T2.Print();
Console.Read();
}
}
Output
A = 80 B = 90
A = 80 B = 90
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 for Static Constructor
using System;
class Test
{
public Test()
{
Console.WriteLine("Instance Constructor invoked");
}
static Test()
{
Console.WriteLine(" Static Constructor invoked");
}
}
class StaticConstructorTesting
{
static void Main()
{
//Static Constructor and instance constructor, both are invoked for first instance.
Test T1 = new Test();
//Only instance constructor is invoked.
Test T2 = new Test();
Console.Read();
}
}
}
Output
Static Constructor invoked
Instance Constructor invoked
Instance Constructor invoked
Private Constructor : You can also create a constructor as private. When a class contains at least one private constructor, then it is not possible to create an instance for the class. Private constructor is used to restrict the class from being instantiated when it contains every member as static.
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