In this blog we will discuss about the constructors in c# and their different types in detail
- We generally know that all the values that has been created must be given values.
- We have don this with two approaches before:
- the first one, uses the dot operator to access the variables and assign the values the individually.
- the second one, is by using functions like GetData to initualize each object individually using statements like:
rect1.GetData(2,4);
- It would be much simpler to initialize objects when they are created.C# supports this type of process using the concept known as constructors.
- A constructor initializes an object when it is created.
Properties of constructors:
- Constructors has the same name as the class name itself.
- They do not specify the return type, not even void .
Let us understand this concept with a simple example:
class Rectangle { public int length; public int width; public Rectangle ( int x ,int y ) //constructor method-values passed from main method are sent here { length = x; // values are assigned to local variables length and width width = y; } public int RecArea() { return ( length*width ); } }
Types of constructors in c#
- overloaded constructors
- static constructor
- private constructors
- copy constructors
- destructors
Overloaded constructors in c#
- It is possible for us to create methods with same name but different parameter list and definitions.This is called as Method overloading.
- Method overloading is used when we want to perform similar tasks with different input parameters.
- When we call a method using an object , first c# matches with the method name and then checks with the number of parameters in order to decide which definition to execute.This process is known as polymorphism.
Example for overloaded constructor:
code:
class Building { public double length; public double breadth; public Building ( double x , double y ) //constructor 1 { length = x; breadth = y; } public Building( double x ) // constructor 2 { length = breadth = x; } public int Area() { double area; Console.WriteLine( " area is " + area ); } public static void Main() { Building b1 = new Building( 10.0 , 2.0 ); // using constructor 1 Building b2 = new Building ( 20.0 ); // using constructor 2 b1.Area(); } }
output
area is 20
Static constructors in c#:
- We can have static constructors as we can have static members.
- A static constructor is called before any object of class is created.
- It is usually used to assign initial values to static data members.
- A static constructor is created by adding the static keyword before the constructor name.
- It cannot have any parameters.
Example:
class Xyz { static Xyz() // no parameters { . . . . // set values to the static members here } }
Note:
- There is no access modifier on static constructors.It cannot take any.
- A class may have only one static constructor.
Private constructors in c#:
- c# does not have any global variables or constants.
- All declarations must be present within a class.
- Sometimes we want to create a class for static members, such classes are never required to instantiate objects.
- We can prevent object creation for such classes by adding the private keyword before the class name.
Copy constructors in c#:
- A copy constructor creates an object by copying the variables from another object.
- C# does not provide this copy constructor feature, if we want to it we have to provide it ourselves.A copy constructor feature can be added as follows:
code
using System; class Items { public Item ( Item item ) // declaring copy constructor { code = item . code; price = item . price; } public Item( int x , double y ) { code = x; price = y; } public String Details // get details of employee { get { return " the code is " + code.toString() " the price is " + price .toString() } } } class Abc { public static void Main() { Item item1 = new Item ( 1 , 200.0 ); Item item2 = new Item ( item1); here item1 details are copied to item2 Console.WriteLine ( item2.details ) } }
Output:
the code is 1 the price is 200
Destructor in c#
- A destructor is the opposite of constructor.It is a method that is called when the object is no longer required
- The name of the destructor is same as the class name and it is preceded by the tilde symbol (~ ).
- Like constructor the destructor also has no return type.
Example:
class Tesla { ~ Tesla() // no arguments { } }
- c# manages the memory dynamically and uses garbage collector, running on a separate thread, to execute all destructors on exit.
- The process of calling the destructor when an object is reclaimed by the garbage collector is called finalization.