C# Constructor

C# Constructors

Constructors

A constructor is a special method that is used to initialize objects. The advantage of a constructor, is that it is called when an object of a class is created. It can be used to set initial values for fields.

Object creation is not enough compulsory we should perform initialization then only the object is in a position to provide the response properly.

Whenever we are creating an object some piece of the code will be executed automatically to perform initialization of an object this piece of the code is nothing but constructor.

Hence the main objective of constructor is to perform initialization of an object.

Example:-

Create a constructor:

// Create a Car class

class Car

{

public string model; // Create a field

// Create a class constructor for the Car class

public Car()

{

model = "Mustang"; // Set the initial value for model

}

static void Main(string[] args)

{

Car Ford = new Car(); // Create an object of the Car Class (this will call the constructor)

Console.WriteLine(Ford.model); // Print the value of model

}

}

Note:-

1. constructor name must match the class name, and it cannot have a return type (like void or int).

2. constructor is called when the object is created.

3. All classes have constructors by default: if you do not create a class constructor yourself, C# creates one for you. However, then you are not able to set initial values for fields.

4. Constructors save time!

Constructor Parameters:-

Constructors can also take parameters, which is used to initialize fields.

The following example adds a string modelName parameter to the constructor. Inside the constructor we set model to modelName (model=modelName). When we call the constructor, we pass a parameter to the constructor ("Mustang"), which will set the value of model to "Mustang":

Example:-

class Car

{

public string model;

// Create a class constructor with a parameter

public Car(string modelName)

{

model = modelName;

}

static void Main(string[] args)

{

Car Ford = new Car("Mustang");

Console.WriteLine(Ford.model);

}

}

Example:-

class Car

{

public string model;

public string color;

public int year;

// Create a class constructor with multiple parameters

public Car(string modelName, string modelColor, int modelYear)

{

model = modelName;

color = modelColor;

year = modelYear;

}

static void Main(string[] args)

{

Car Ford = new Car("Mustang", "Red", 1969);

Console.WriteLine(Ford.color + " " + Ford.year + " " + Ford.model);

}

}

Key point: Just like other methods, constructors can be overloaded by using different numbers of parameters.

Without constructor:

Program.cs

class Program

{

static void Main(string[] args)

{

Car Ford = new Car();

Ford.model = "Mustang";

Ford.color = "red";

Ford.year = 1969;

Car Opel = new Car();

Opel.model = "Astra";

Opel.color = "white";

Opel.year = 2005;

Console.WriteLine(Ford.model);

Console.WriteLine(Opel.model);

}

}

With constructor:

Program.cs

class Program

{

static void Main(string[] args)

{

Car Ford = new Car("Mustang", "Red", 1969);

Car Opel = new Car("Astra", "White", 2005);

Console.WriteLine(Ford.model);

Console.WriteLine(Opel.model);

}

}

Ebook Download
View all
Learn
View all