C# Constructor –Learn with Programming Example

In this chapter you will learn:
  • What is Constructor?
  • How to initialize Constructor?
  • Programming Example of C# Constructor?

What is Constructor?

A constructor is a special method which is used to initialize object when it is created. To understand its definition we take an example.

You are creating a class in which you have required a temperature value in centigrade and in then use that value in entire class. A constructor is used here for initializing an object with value.

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

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

        public CalculateHeat(string Message)
        {
            Console.WriteLine(Message.ToString());
        }

        public void calculate()
        {
            decimal fahrenheit = Convert.ToDecimal(temp * 1.8 + 32);
            Console.WriteLine("{0} degree centigrade into fahrenheit = {1}", temp, fahrenheit);
        }

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

            CalculateHeat c2 = new CalculateHeat("I am second constructor");          
        }
    }
}

Output

36 degree centigrade into fahrenheit = 96.8
I am second constructorDestructor Initializes, Cleanup Process Complete
__
 

Explanation

Constructor Creating: In this program, we have created two constructors and both need a parameter passed to it while initialize object. One constructor is for getting integer value and another one is for displaying message on screen. Notice that both constructors have same name as class but have different parameter. You may create as much constructor as you need with different parameter or number of parameter.
 //Constructor Creation
 public CalculateHeat(int val)
 {
     temp = Convert.ToInt32(val);
 }

 public CalculateHeat(string Message)
 {
     Console.WriteLine(Message.ToString());
  }

Object Initialization: In the Main method we have initialized object and while initialization of object we passed parameter in it.
 public static void Main(String[] args)
 {
   CalculateHeat cl = new CalculateHeat(36);
   cl.calculate();

   CalculateHeat c2 = new CalculateHeat("I am second constructor");            
 }

Destructor: We have also used Destructor in this program to free up system resources and cleanup memory. It is good habits to use destructors to avoid security breaches.
//Destructor Initialization, It will clean memory at the end and frees up system resources.
 ~CalculateHeat()
 {
    Console.WriteLine("Destructor Initializes, Cleanup Process Complete");
    Console.ReadLine();
 }

Summary

In this chapter we have learned how to use constructor and destructor in c sharp programming. In the next chapter you will learn Destructor (~)

More Articles