Introduction
The first way we learned to approach object instantiation was with the constructor method in the class. This is the tried and true (and only) way to actually make an instance of a class. The constructor is a method with no return type declared and has the same name as the class it is declared in.
Right about now, you're probably already thinking, "Hey, wait a minute! What about the many approaches you mentioned?!?" Well, all the different approaches are really just different ways to control where (and when) we new up our objects, and I'll get to these cool tricks in Part II after we cover the basics.
The Default Constructor
So, to get started, we declare a constructor as follows.
public class One
{
public One()
{
// Constructor code here
}
public int Number { get; set; } // This is a property, assuming you want to define a property named "Number"
}
We new-up (instantiate) the object using the constructor we defined.
OneobjOne=newOne();
Every time we make a new instance of the "One" class, the constructor method will be run. If there is actually code in the constructor, it will be executed on instantiation of the object.
When the following class is instantiated.
public class Number
{
protected Number()
{
Console.WriteLine(GetType().ToString() + " built");
}
}
We'll get a call to the constructor and output to the console.
With C#, if no constructor is defined, we get a default constructor by default. This default constructor will have no parameters and will not do anything and essentially be the same as if we had declared an empty constructor, so these two class declarations are equivalent.
No constructor defined (ends up the same as below).
publicclassOne:Number{}
Default constructor defined (ends up the same as above).
publicclassOne:Number
{
publicOne(){}
}
Changing the Default Constructor
Also, with C#, as soon as we declare a constructor with a signature different than the default constructor, we no longer get a default constructor like in the following two examples.
The default constructor has no parameters, so as soon as we declare an input parameter requirement on the constructor, we lose the default constructor.
publicclassOne:Number
{
publicOne(intx){}
}
In this case, we are now required to pass an integer on instantiation.
OneobjOne=newOne(3);
Accessor change in Object instantiation
The default constructor is public, so as soon as we declare that the constructor is private to the "One" class, we can no longer instantiate the object from outside the class. This probably seems strange right now, but we'll discuss how to use this in a bit.
publicclassOne:Number
{
privateOne(){}
}
Providing Multiple Constructors
We can provide multiple constructors, so if we want to have the choice of whether to supply a value to the constructor or not, we'll just write two of them.
public class One
{
private One()
{
// First private constructor
}
private One(int x)
{
// Second private constructor with a parameter 'x'
}
}
This will allow us to instantiate by passing nothing or passing an integer when we new up the "One" class, so the following two declarations are now valid.
OneobjOne=newOne(3);
OneobjOne=newOne();
Using Constructors
By now, you're probably asking, "OK... Real nice constructors -n- all. I get it. But what's the point, really?"
The point of constructors is to provide any initialization that the class needs. Let's say we have a property that tells us when the object was instantiated, as in the following class.
Praveen KumarPosted Jul 6, 2017, 1:27 AM
Is any difference between One objOne = new One(); and new One().One()
Nandkishore NairPosted Jul 31, 2007, 3:09 AM
Hi, just wondering as to why is that the derived class constructor is called ahead of the base constructor in C#. Is'nt it the opposite way in C++?