Posted by Simplify DotNet on December 27, 2020 in C#, constructors, parameterless constructor | No comments
Constructors
Constructors are used to initialize the data members of when an instance of a class is created. Name of the constructor is always same like class name. Find the example of an constructor below.
class Test
{
public Test() -------> Constructor
{
}
}
As shown in the above example, we have class named as Test and then we have constructor with same name, constructor are like a method in a class but they have a same name as that of class with no return type.
Few important points to remember regarding constructors in C#.
- Constructor have same name as that of class.
- Constructor do not have any return type, not even void.
- There can be more than one constructor for a class.
- Static constructor can not have any parameters and also they do not have any access modifier.
There are different types of constructors as mentioned below.
- Default Constructor
- Parameterized Constructor
- Copy Constructor
- Private Constructor
- Static Constructor
Let us now look different types of constructors one by one.
Default Constructor:
Even if a class do not have any constructor then also a default constructor is automatically created by the compiler. Automatically created default constructor do not accept any parameters. If we create a constructor in a class then always that constructor is called whenever we create an instance of an object.
Let us look at an example of default constructor.
public class Test
{
public string str;
public Test()
{
str = "Hello From Constructor";
}
}
public class Main
{
public void Demo()
{
Test tst = new Test();
Console.Writeline(tst.str)
}
}
As shown in the above example we have created a class Test and its constructor and then initialized one of the data member of the class inside the constructor. Once an instance of the class is created then the constructor is called automatically and then the in the output we will see as "Hello From Constructor".
Parameterized Constructor:
Using parameterized constructor we can initialize the data members of the class for that object, while creating an object for a class we can pass the values through which we can initialise the data members for that class. Let us look at this with an example below.
public class Test
{
public int a;
public int b
public Test(int _a, int _b)
{
a = _a;
b = _b;
}
}
public class Main
{
public void Demo()
{
Test tst = new Test(10,20);
Console.Writeline(tst.a.ToString());
Console.Writeline(tst.b.ToString());
}
}
As shown in the above example we have created a class Test and its constructor and then initialized the data member of the class inside the constructor using the parameters passed to the constructor. Once an instance of the class is created then the constructor is called by passing those two parameters and then in the output we will see as 10 & 20.
Copy Constructor:
Copy constructor is used to create an exact copy of an object by passing that object as a parameter to the constructor. Please find the example of copy constructor below.
public class Student
{
public string _firstName;
public string _lastName;
public Student(Student std)
{
_firstName = std.firstName;
_lastName = std.lastName;
}
public Student(string firstName, string lastName)
{
_firstName = firstName;
_lastName = lastName;
}
}
public class Main
{
public void Demo()
{
Student st = new Student("Prasad","Sonawane");
Student st1 = new Student(st);
Console.Writeline(st1.firstName);
Console.Writeline(st1.lastName);
}
}
As shown in the above example we have created a class Student and there are two types of constructor for the class, one is parameterised constructor and the other one is copy constructor so as we can see we are passing the whole object as parameter to the constructor and in the output we can see as "Prasad Sonawane".
Private Constructor:
Private constructor can not be called from outside the class, one of the practical example for which private constructor can be used is to initialise the static members of the class.
Static Constructor:
Static constructor can be used to initialise the static members of the class, and in the static constructor we can not pass the parameters. Static constructor can not be called explicitly but it is executed during the creation of the first instance of the class or if any of the static members of the class is referenced. Static constructor is declared using static keyword. Below is the example of the static constructor.
public class Student()
{
static Student()
{
}
}
So today we have looked what is constructor in C#, how it can be used and its importance, also we have looked in different types of constructors.
For any issues related to C#, ASP.NET, jQuery or to provide any feedback please write to me on prson94@gmail.com, I will try my best to reply you as soon as possible.
Thank You...….
Email Id - prson94@gmail.com.
0 comments:
Post a Comment