Constructors In C#

I am going to dedicate this blog to Constructors in C#. I am going to give very simple and short example of constructors. So very first question that arise in your mind about constructor is what the constructors are? And the answer is very simple, Constructors are special type of methods which invoke when you create the instance of a class.

Now next question is what is the use of constructors? And answers is,Constructors are used to initialize the members of the class. Very Important thing about constructor is that it has the same name as class. Means if a class name is Demo then the constructor name is also Demo.

Now consider a function in which you define two functions one is for initialize the variables and second function is for do some type of calculation. Here is the simple example of this kind of function

  1. using System;  
  2. class calculator {  
  3.     static int number1, number2, total;  
  4.     public void Initialize() {  
  5.         number1 = 10;  
  6.         number2 = 20;  
  7.     }  
  8.     public void AddNumber() {  
  9.         total = number1 + number2;  
  10.     }  
  11.     public void Display() {  
  12.         Console.WriteLine("The total is" + total);  
  13.     }  
  14.     public static void Main(string[] agrs) {  
  15.         calculator calc = new calculator();  
  16.         calc.Intialize();  
  17.         calc.AddNumber();  
  18.         calc.Display();  
  19.     }  
  20. }  

Now study the above code and check we add three functions in the class one is for Initialize the variables (Intialize()), second is for add these numbers (AddNumber()), and third one is for display the output (Display()) and after write all the code we call all functions in Main method. Its look simple and clean. But we can do it more simple and easy.

Let us use constructors for Initialize variables. For this now try this code

  1. using System;  
  2. class calculator {  
  3.     static int number1, number2, total;  
  4.     calculator() {  
  5.         number1 = 10;  
  6.         number2 = 20;  
  7.     }  
  8.     public void AddNumber() {  
  9.         total = number1 + number2;  
  10.     }  
  11.     public void Display() {  
  12.         Console.WriteLine("The total is" + total);  
  13.     }  
  14.     public static void Main(string[] agrs) {  
  15.         calculator calc = new calculator();  
  16.         calc.AddNumber();  
  17.         calc.Display();  
  18.     }  
  19. }  

Have you find any difference? If yes then you learned Constructors very well. If not then let me tell you. Check the calculator() constructor in this class. I use calculator constructor instead of Initialize function. Now check the Main method. Oh yes! There is not any function like Initialize(). As I told you above. Constructors invoked when a new class instanced. Whenever you create an object of class the construct invoked itself. You do not have to invoke it separately. Now I hope you understand the concept of Constructors very well. Guys Practice c# , enjoy this awesome language and have a wonderful future. I just want to tell you one thing. Never ever lose your trust on your language or technologies which you choose for your career. I see many developers who switch to different technologies just because they got few bugs more than this awesome language. But C# and .net have awesome scope and have a wonderful future. Believe in yourself, with all of these words I want to say good day to all of you. I will come back with new article soon. Till then enjoy this awesome website which has an awesome name C-sharp corner.