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

using System;
 

class calculator

{

    static int number1,number2,total;           

    public void Initialize()

    {

        number1 = 10;

        number2 = 20;

    }

    public void AddNumber()

    {

         total=number1+number2;

    }

    public void Display()

    {

         Console.WriteLine("The total is"+total);

    }

    public static void Main(string[] agrs)

    {

         calculator calc= new calculator();

         calc.Intialize();

         calc.AddNumber();

         calc.Display();

    }

}

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

using System;
 

class calculator

{

     static int number1,number2,total;

     calculator()

     {

           number1=10;

            number2=20;

    }

    public void AddNumber()

    {

        total=number1+number2;

    }

   public void Display()

   {

       Console.WriteLine("The total is"+total);

   }

   public static void Main(string[] agrs)

   {

        calculator calc= new calculator();

        calc.AddNumber();

        calc.Display();

    }

}

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.

Ebook Download
View all
Learn
View all