C Sharp

Constructors

One of the biggest advantages of an OOP language such as C# is that you can define special methods that are always called whenever an instance of the class is created. These methods are called constructors. C# introduces a new type of constructor called a static constructor, which you'll see in the next section, "Static Members and Instance Members." -

A key benefit of using a constructor is that it guarantees that the object will go through proper initialization before being used. When a user instantiates an object, that object's constructor is called and must return before the user can perform any other work with that object. It's this guarantee that helps ensure the integrity of the object and helps make applications written with object-oriented languages much more reliable.

But what do you name a constructor such that the compiler will know to call it when the object is instantiated? The C# designers followed the lead ofStroustrup and dictated that constructors in C# must have the same name as the class itself. Here's a simple class with an equally simple constructor: -

using System;
class Constructor1App
{
    Constructor1App()
    {
        Console.WriteLine("I'm the constructor.");
    }
    public static void Main()
    {
        Constructor1App app = new Constructor1App();
    }
}

Constructors do not return values. If you attempt to prefix the constructorwith a type, the compiler will emit an error stating that you cannot define members with the same names as the enclosing type.

You should also note the way in which objects are instantiated in C#. This is done using the new keyword with the following syntax: -

<class> <object> = new <class> (constructor arguments) -

If you come from a C++ background, pay special attention to this. In C++, you can instantiate an object in two ways. You can declare it on the stack, like this: -

// C++ code. This creates an instance of CMyClass on the stack.
CMyClass myClass;

Or you can instantiate the object on the free store (or heap) by using the C++ new keyword: -

// C++ code. This creates an instance of CMyClass on the heap.
CMyClass myClass = new CMyClass();

Instantiating objects is different in C#, and this is a cause for confusion for new C# developers. The confusion stems from the fact that both languages share a common keyword for creating objects. Although using the new keyword in C++ lets you dictate where an object gets created, where an object is created in C# depends upon the type being instantiated. As we saw in Chapter 4, reference types are created on the heap and value types are created on the stack. Therefore, the new keyword lets you create a new instance of a class, but it doesn't determine where that object is created.-

Having said that, the following code is valid C# code, but if you're a C++ developer it doesn't do what you might think: -

MyClass myClass;

In C++, this would create an instance of MyClass on the stack. As mentioned, you can create objects in C# only by using the new keyword. Therefore, this line of code in C# merely declares that myClass is a variable of type MyClass,but it does not instantiate the object.

As an example of this, if you compile the following program, the C# compiler will warn you that the variable has been declared but is never used in the application: -

using System;
class Constructor2App
{
    Constructor2App()
    {
        Console.WriteLine("I'm the constructor");
    }
    public static void Main()
    {
        Constructor2App app;
    }
}

Therefore, if you declare an object type, you need to instantiate it someplace in your code by using the new keyword: -

Constructor2App app;
app = new Constructor2App();

Why would you declare an object without instantiating it? Declaring objects before using them-or " new-ing" them-is done in cases in which you're declaring one class inside of another class. This nesting of classes is called containment, or aggregation. -