Back to course

Constructors and Parameterized Constructors

.NET Zero to Hero: Master C# and Modern App Development

Constructors

A constructor is a special method that is called when an object is created. It is used to initialize the object's data.

Default Constructor:

csharp class Car { public Car() { /* Code here */ } }

Parameterized Constructor:

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

// Creating object Car myCar = new Car("Mustang");