Home  • Programming • C#.NET

Using Constructors in C# Programming

Constructors are class methods that are executed when an object of a given type is created. Constructors have the same name as the class, and usually initialize the data members of the new object. In the following example, a class called Taxi is defined with a simple constructor. This class is then instantiated with the new operator. The Taxi constructor is invoked by the new operator immediately after memory is allocated for the new object.
public class Taxi
{
    public bool isInitialized;
    public Taxi()
    {
        isInitialized = true;
    }
}

class TestTaxi
{
    static void Main()
    {
        Taxi t = new Taxi();
        System.Console.WriteLine(t.isInitialized);
    }
}
A constructor that takes no parameters is called a default constructor. Default constructors are invoked whenever an object is instantiated using the new operator and no arguments are provided to new.
class NLog
{
    // Private Constructor:
    private NLog() { }

    public static double e = System.Math.E;  //2.71828...
}

Comments 3


oh.. very simple
it clears the conception of Constructor.....Thanks Sir.
when we create any object from any class it is often refers to as an object is instantiated

Share

Copyright © 2024. Powered by Intellect Software Ltd