Back to course

Constructors

C# Zero to Hero: Comprehensive Programming Masterclass

Class Constructors

A constructor is a special method that is used to initialize objects. It is called when an object of a class is created.

Key Features

  • Must have the same name as the class.
  • Does not have a return type.

Example

csharp class Car { public string model;

public Car(string modelName) 
{
    model = modelName;
}

}

Car myCar = new Car("Tesla");