In this article I am going to discuss about C# Constructor in detail but before that if you had not read my previous article of c# types of classes then must read it for more clear understanding. If you had heared the term constructor then must got it what am talking about. But if you don’t know about constructor and how to use this in C# then don’t worry you are in right place. So lets start the concept of constructor with the introduction.
Introduction to C# Constructor
In Object-Oriented programming we write our whole code inside the single class and then access the properties of class by creating the instance of that class. A class contain data members, member functions and constructors. Inside a class constructors play crucial role because it is used to initializing the variables of that class. Constructors are also called special type of methods that is present inside the class. In C# the name of constructor must be same as the name of class. If you not declare the constructor with the same name of class then you get an error. Remember constructor does not have any return type therefore it will not return any value. You can declare constructor like following :
public class Student { public Student() //pass parameters { // Your initialization } }
Important Points Regarding C# Constructor
- In C# we can declare the constructor either implicitly or explicity.
- Implicit Constructors are basically constructor without any parameters.This type of constructor is also knows as default constructor (one of types of C# constructor).
- And, Explicit Constructors are constructor with any number of parameters. For invoking this type of constructor we have to manually write inside the class.
- Constructor does not have any return type. Hence, it will not return any value.
- The name of constructor must be same as the name of class.
- We can also declare private and static constructors
Program to Explain Implicit Constructor
using System; namespace ImplicitConstructor { class Program { static void Main(string[] args) { Test obj = new Test(); Console.WriteLine("Number = "+obj.number); Console.WriteLine("Learn web development from = "+obj.name); Console.ReadKey(); } } class Test { public int number=10; public string name="webtutorialstack.com"; } }
Output:
Number = 10
Learn web development from = webtutorialstack.com
In above program we does not have created any constructor but here the default constructor will invoked automatically because default constructor does not contain any parameter and called automatically when the instance of class is created. In my case when i create the object of test class then constructor will be called and initialize the variables so i can access through dot operator.
Program to explain Explicit Constructor
using System; namespace ExplicitConstructor { class Program { static void Main(string[] args) { Test obj = new Test(); } } class Test { public Test() { Console.WriteLine("Constructor Called"); } } }
Output:
Constructor Called
In above program I had explicitly made one constructor which simply prints the constructor called message. So hope you clear what exactly the difference between implicitly defined constructor and explicitly defined constructor.
Conclusion
In this C# tutorials article I have discussed about constructor and how we can call them implicitly and explicitly. Hope you understand this and like this article. In my next article I am going to cover all the types of constructors with proper examples so you get to know when to use which type of constructor without any problem. If you want to know more about csharp constructor then you can also visit official documentation.