Class Constructor in C#

Introduction

Class Constructor is a particular method which is invoked automatically at the time of object creation.  Besides, Class Constructor does not return anything.

In C#, a class can have multiple class constructors.

Function of Class Constructor

  • Set default value of a class’ properties
  • The Initialisation, which initialises the data members (properties) of the new objects

Default Constructor

Default Constructor is a constructor (special method) without any parameters. The class will invoke Default Constructor when an instance of the class is created. 

In runtime, every instance of the class will be initialised to the same value

  • All numeric filed (int, float etc.) will be 0
  • String or other objects will be null

Talk Is Cheap, Show Me The Code

As you can see, we create a Student class in the code and initialise a UniStudent instance of this class.

Line 20 – 23: This is a Default Constructor of Student Class.  You may have been aware of that; the default class does not have any parameters.


Parameterized Constructor

Compared with Default Constructor, Parameterized Constructor has at least one parameter; as a result, each instance of the class may carry different value.

Talk Is Cheap, Show Me The Code

As you can see, we create a Parameterized Constructor and pass in two parameters, name and studentID.

Line 19 – 23: we create a Parameter Constructor. The Parameter Constructor has two parameters: name and studentID.


Copy Constructor

Copy Constructor creates an object by copy variables from anther object. In other word, it initialised a new instance whose properties’ value is as same as an existing instance.

Talk Is Cheap, Show Me The Code

Line 19 – 24: We create a Parameterized Constructor and it has two parameter, name and studentID.

Line 26 – 31: We create a Copy Constructor and it has one parameter, which is an instance of Student class. Inside the Copy Constructor, we can retrieve student instance’s properties’ value and assign them to Student’s properties, respectively.


Static Constructor

Static Constructor is used to initialise any static data in class, it performs an action that needs to be performed once only. It can be called directly.

  • Static Constructor gets called first when any static properties is accessed.
  • It is called automatically to initialise the class before the first instance is created or any static members are referred
  • Users have no control over when the Static Constructor is executed
  • Only one Class Constructor is allowed to create

Talk Is Cheap, Show Me The Code

Console Output

In the above example, we create a Static Constructor and a Default Constructor. The Static Constructor will be invoked only once for the first time of class.

We call the Student() method twice in the above code. We just call the Default Constructor when we implement the Student() method on second time.


Private Constructor

Private Constructor is a special instance constructor. A class only contain a or multiple Private Constructor are not allowed to create an instance for the class.

It can be quite useful in Singleton Design Pattern. For example, the GPS Locator Service in the app.


Leave a Reply

Your email address will not be published. Required fields are marked *