Makes you understand the purpose of constructors in C#, and shows you how to use them.
Beginner C# programmers tend to get a bit confused about constructors. They don’t know how to use it, what’s it good for…
If you programmed C# Console Applications, probably when you created a class you didn’t even used a constructor. I said when you programmed Console Applications because the forms in a Windows Application already have a constructor for the class named by default Form1 created by Visual Studio .NET.
Anyway, the important thing is that if you are reading this, you want to know what’s the deal with constructors.
Let’s create a new Console Application project named usingConstructors.
Class file
Class1.csis created by Visual Studio .NET and it contains the following code:
using System;
namespace usingConstructors
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
}
}
}
The code is composed of namespace usingConstructors, the class Class and Main() method.
We’ll now create our own class inside the usingConstructors namespace, named TempClass. The class has inside a variable of type int named Temperature, and a method that writes Temperature is + the value of the Temperature variable:
using System;
namespace usingConstructors
{
// Our new class named TempClass
class TempClass
{
// Variable inside a class
private int Temperature;
// Method ShowTemp() inside the class
public void ShowTemp()
{
// The method shows the temperature
Console.WriteLine("Temperature is " + Temperature);
}
}
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// Instantiating an instance of TempClass named tc
TempClass tc = new TempClass();
tc.ShowTemp();
}
}
}
Also you can see we created a new instance of the TempClass class in the Main() method of Class1.
Compile the program with CTRL+F5 and watch the result:
Temperature is 0
Press any key to continue
The result is expected, as the Temperature variable has no value assigned.
To assign a value to the Temperature variable we can make the method ShowTemp() to take parameters, but there’s a better way: using a constructor.
This is the final code, we add a constructor to the class TempClass() and use it in the Main() method of Class1 to change the value of Temperature variable.
First code, ask questions after:
using System;
namespace usingConstructors
{
// Our new class named TempClass
class TempClass
{
// Variable inside a class
private int Temperature;
// The constructor of the class
public TempClass(int temp)
{
// We assign the value of the temp parameter
// to the Temperature variable
Temperature = temp;
}
// Method ShowTemp() inside the class
public void ShowTemp()
{
// The method shows the temperature
Console.WriteLine("Temperature is " + Temperature);
}
}
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// Instantiating an instance of TempClass named tc
TempClass tc = new TempClass(90);
tc.ShowTemp();
}
}
}
It may be hard to spot what we added / modified to the code, so that’s why it is bold.
First, you may ask, how does the compiler know that TempClass() is the constructor of the class and not some method?
Simple, the name is identical to the name of the class! A constructor is a constructor only if it has the same name as the class in which he resides.
Further you can see that the constructor has one parameter, an integer variable named temp. I suppose you already know what a parameter is and how it works from methods, if not go learn the C# methods first. Inside the constructor the private Temperature variable of class TempClass is assigned to the value of the parameter.
In the class Class1 method Main now instantiates a new object tc from the TempClass class with the parameter 90.
Test the program now and watch the result:
Temperature is 90
Press any key to continue
In conclusion, the purpose of a constructor is to initialize member variables within the class.
Even if you don’t create a constructor at all, the compiler creates one for you, as an instantiated object without a constructor cannot have a valid state.
This may have been a lousy example, but we got the job done with it (a more useful method of TempClass would transform the temperature from Fahrenheit degrees to Celsius).