Constructors and Destructors in C#.Net

Constructor

Constructors are special methods called when a class is instantiated.

  • Constructor will not return anything.
  • Constructor name is same as class name.
  • By default C# will create default constructor internally.
  • Constructor with no arguments and no body is called default constructor.
  • Constructor with arguments is called parameterized constructor.
  • Constructor by default public.
  • We can create private constructors.
  • A method with same name as class name is called constructor there is no separate keyword.

Sample Program

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

namespace BRK.ConstructorExample
{
    class Welcome
    {
        // Default constructor
        public Welcome()
        {
            Console.WriteLine("Welcome message from Default Constructor...");
        }

        // Parametarized constructor
        public Welcome(string name)
        {
            Console.WriteLine("\n\nThis message from parametarized constructor");
            Console.WriteLine("Welcome to Constructor sample, by {0}", name);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Creating object for Welcome class
            // This will called default constructor
            Welcome obj = new Welcome();

            // Creating object for welcome class by passing parameter
            // This will called parametarized constructor which matches
            Welcome pObj = new Welcome("Ramakrishna Basgalla");

            Console.Read();
        }
    }
}

Output

const1.gif

Private Constructor

As I mentioned above we can create private constructors, but we have limitation in using that. The following program shows how to create and use a private constructor.

Sample Program

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

namespace BRK.PrivateConstructorSample
{
    class Welcome
    {
        // Default private constructor
        private Welcome()
        {
            Console.WriteLine("Welcome message from Default Private Constructor...");
            Console.WriteLine("Created by Ramakrishna Basagalla (-:");
        }

        static void Main(string[] args)
        {
            // Creating object for Welcome class
            // This will called default private constructor
            Welcome obj = new Welcome();

            Console.Read();
        }
    }
}

Output

const2.gif

To use a private constructor we should have main function in the same class, generally we will define constructors in different classes so defining private constructors is not that much useful.

Static Constructor

A static constructor has the same name as the class name but preceded with the static keyword; it will be called at the time of class load.

  • No access specifier for static constructor.
  • Static constructor will not return anything.
  • Static constructor will accept only static members.
  • Static constructor will call at the time of class loading.
  • Static constructor will not allow overloading, so there is no parameterized static constructor.

Sample Program

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

namespace BRK.ConstructorExample
{
    class Welcome
    {
        public static string Name = "Ramakrishna Basagalla";

        // Static Constructor
        static Welcome()
        {
            Console.WriteLine("Welcome message from static Constructor...");
            Console.WriteLine("{0} name is coming from static member",Name);
        }

        // Parametarized constructor
        public Welcome(string name)
        {
            Console.WriteLine("\n\nThis message from parametarized constructor");
            Console.WriteLine("Welcome to Constructor sample, by {0}", name);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Creating object for welcome class by passing parameter
            // This will called parametarized constructor which matches
            Welcome pObj = new Welcome("Ramakrishna Basgalla");

            Console.Read();
        }
    }
}

Output

const3.gif

In the above program we have created parameterized object for Welcome class, here the static constructor is called at the time of class load after then it's called corresponding parameterized constructor.

Destructors

.Net will clean up the un-used objects by using garbage collection process. It internally uses the destruction method to clean up the un-used objects. Some times the programmer needs to do manual cleanup.

Syntax

~<ClassName>
{}

Sample Program

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

namespace BRK.ConstructorExample
{
    class Welcome
    {
        // Default constructor
        public Welcome()
        {
            Console.WriteLine("Welcome message from Default Constructor...");
        }

        // Destructor
        ~Welcome()
        {
            Console.WriteLine("Destructor called");       
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Creating object for Welcome class
            // This will called default constructor
            Welcome obj = new Welcome();

            Console.Read();
        }
    }
}

Note: Destructor will call after execution of the program.

Let me know if you need any other technical articles in .Net.

HAPPY CODING.


Similar Articles