C# Constructor Details
- Use class name. A method with the same name as the class.
- No return value.
- Overloadable. You can overload your constructors.
- Optional. When you create classes, you do not have to create a class constructor. If you do not, the constructor for the parent class is called.
- Static Constructors. You can make a constructor static (class member fields are initialized by the CLR prior to any object instantiations).
Overloading C# Constructors
You can overload a constructor with various parameters that initialize object instances to various states.
public class Cyborg
{
public string CyborgName;
//Constructor with no params.
public Cyborg() { }
//Constructor with a parameter.
public Cyborg(string pName)
{
CyborgName = pName;
}
}
Working WinForms Example
The following demonstrates using a finalizer and System.GC.Collect(). I placed a MessageBox.Show() in the finalizer so you can see when it is called. The finalizer gets called when the compiler needs to free resources or you call System.GC.Collect(). You'll notice that although the local variable in the button click event falls out of scope, your finalizer is not called until either you close the form or click the button that calls System.GC.Collect().
Create a form with two buttons and add code as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CR_Constructor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Use class calling no parameter constructor.
Cyborg MyRobot1 = new Cyborg();
MyRobot1.CyborgName = "John";
MessageBox.Show("My robot's name is " + MyRobot1.CyborgName);
//Use the class calling an overloaded
//constructor with a single parameter.
Cyborg MyRobot2 = new Cyborg("Cameron");
MessageBox.Show("My robot's name is " + MyRobot2.CyborgName);
}
}
public class Cyborg
{
public string CyborgName;
//Constructor with no params.
public Cyborg() { }
//Constructor with a parameter.
public Cyborg(string pName)
{
CyborgName = pName;
}
}
}
Static Constructors
You can make a constructor static. With a static constructor, the CLR initializes the static class member fields prior to any object instantiations. You can have only one static constructor with no parameters.
public class Cyborg
{
//Executes once per program.
static Cyborg() { }
}
You can have a static constructor and a regular constructor. The static constructor executes once prior to any object instantiations then the regular constructor executes every time you instantiate an object. At first glance of the following code, you might think it will not compile because it breaks the rules of overloading. However, it doesn't break any rules because each method is called at a different time.
public class Cyborg
{
public string CyborgName;
//Executes once per program. Notice no access modifier.
static Cyborg() { }
//Executes once per object instantiation.
public Cyborg() { }
}