Hi folks, In this tutorial, We are going to learn What is Constructor in C#? A Constructor is a special type of method which is invoked when we create an instance of the class.

What is Constructor in C#?

A Constructor is a special type of method which is invoked when we create an instance of the class. It has the same name as the class name. A constructor does not have any return type not even void. It is used to assign the initial value to the data member of the class.

For Example, If I have a class called MyClass then the Constructor of the class is created as follows,

public class MyClass

{

public MyClass()

{

//Constructor 

}

}

In the above code snippet, we have a class called MyClass and the constructor of this class is created with the same as the class name.

Types of Constructor:

There are following types of Constructor in C#.

  1. Default Constructor.
  2. Parameterized Constructor.
  3. Copy Constructor.
  4. Private Constructor.
  5. Static Constructor.

We will discuss Default and Parameterized Constructor in this tutorial. Rest of the types we will discuss in next sessions.

Default Constructor:

A constructor with no parameters is called a default constructor. The default constructor initializes all numeric fields to zero and all string and object fields to null inside a class.

using System; 
public class Student 
{ 
public Student() 
{ 
Console.WriteLine("Default Constructor Invoked"); 
} 
} 
class TestStudent{ 
public static void Main(string[] args) 
{ 
Student objStudent1 = new Student(); 
Student objStudent2 = new Student(); 
} 
}

Output:

Default Constructor Invoked

Default Constructor Invoked

Parameterized Constructor:

A Constructor with at least one parameter is called Parameterized Constructor. It can initialize each instance of the class to new values.

using System;

public class Student
{
public int _iRollNo;
public String _sName;

public Student(int iRollNo, String sName)
{
_iRollNo = iRollNo;
_sName = sName;
 
}
public void DisplayRecord()
{
Console.WriteLine("Roll No " + " " + _iRollNo + ". Name " + _sName);
}
} 
class Program
{
 
 
static void Main(string[] args)
{

Student objStudent = new Student(1,"Vinod");
Student objStudent1 = new Student(2, "Ansh");
objStudent.DisplayRecord();
objStudent1.DisplayRecord();
 
Console.ReadLine();
 
}
 
}

Output:

Roll No 1. Name Vinod
Roll No 2. Name Ansh

Point Of Interest for Constructor:

  1. A constructor has the same name as the class name.
  2. A constructor doesn’t have the return type not even void.
  3. A class can have any number of the constructor.
  4. A constructor cannot be declared as abstract final.
  5. A static  constructor cannot be parameterized.

View More:

Conclusion:

I hope you would love this post. Please don’t hesitate to comment for any technical help. Your feedback and suggestions are welcome to me.

Thank You.

Explore post tags

16 Comments

  • Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something. I think that you can do with a few pics to drive the message home a little bit, but other than that, this is great blog. An excellent read. I’ll definitely be back.|

  • Definitely consider that that you said. Your favorite justification seemed to be on the net the simplest thing to take into accout of. I say to you, I certainly get annoyed at the same time as other folks think about worries that they plainly don’t understand about. You managed to hit the nail upon the highest and also defined out the whole thing without having side effect , other folks can take a signal. Will probably be again to get more. Thanks|

  • 731955 937299The article posted was very informative and valuable. You people are performing a fantastic job. Keep going. 182426

  • I have taken notice that in digital cameras, special sensors help to maintain focus automatically. Those kind of sensors associated with some digital cameras change in contrast, while others utilize a beam of infra-red (IR) light, particularly in low lumination. Higher specification cameras often use a mixture of both methods and will often have Face Priority AF where the camera can ‘See’ any face while focusing only in that. Many thanks for sharing your opinions on this weblog.

  • Thanks for this excellent article. Also a thing is that the majority of digital cameras are available equipped with a zoom lens so that more or less of a scene to generally be included by ‘zooming’ in and out. These kinds of changes in focusing length tend to be reflected in the viewfinder and on large display screen on the back of the camera.

  • I know this if off topic but I’m looking into starting my own weblog and was curious what all is required to get setup? I’m assuming having a blog like yours would cost a pretty penny? I’m not very web smart so I’m not 100 positive. Any suggestions or advice would be greatly appreciated. Cheers|

  • 22973 61178Some genuinely good and utilitarian information on this internet site , likewise I feel the layout has wonderful attributes. 559637

  • Remarkable things here. I’m very satisfied to peer your article. Thank you a lot and I’m having a look ahead to contact you. Will you please drop me a mail?|

  • Everything is very open with a very clear clarification of the issues. It was truly informative. Your site is very useful. Many thanks for sharing!|

  • I was recommended this web site through my cousin. I’m not certain whether or not this publish is written by means of him as no one else know such exact approximately my problem. You are incredible! Thank you!|