Home > DeveloperSection > Forums > What is Constructor in c#
Ankit Singh

Total Post:341

Posted on    December-01-2015 9:53 PM

 C# C#  .NET 

 1 Reply(s)
 216  View(s)
Rate this:
 I want to know what is Constructor  and how to use it. please help me.

aditya kumar Patel

Total Post:250

Posted on    December-01-2015 10:04 PM

Constructor is a special method of a class which will invoke automatically whenever instance or object of class is created. Constructors are responsible for object initialization and memory allocation of its class. If we create any class without constructor, the compiler will automatically create one default constructor for that class. There is always at least one constructor in every class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace constructor_initialize
    class Calculate
        public int temp = 0;
        //Constructor Creation
        public Calculate(int val)
            temp = Convert.ToInt32(val);

        public Calculate(string Message)

        public void calculate()
            decimal Sum = Convert.ToDecimal(temp);
            Console.WriteLine(" Initializ value= {0}", temp);

        //Destructor Initialization, It will clean memory at the end and frees up system resources.
            Console.WriteLine("Destructor Initializes, Cleanup Process Complete");
    class Program
        public static void Main(String[] args)
            Calculate cl = new Calculate(2000);

            Calculate c2 = new Calculate(string.Format("MachineName: {0}", Environment.MachineName));


Don't want to miss updates? Please click the below button!

Follow MindStick