Constructors - potatoscript/csharp GitHub Wiki
π₯ Constructors in C# π₯
π₯ What Are Constructors? π₯
A constructor is a special method in a class that is automatically called when you create (or instantiate) an object from that class. Its main job is to initialize the new object. It can set the initial values of the properties or perform any setup work needed for the object.
Think of a constructor as the blueprint that gets followed when you create a new car, person, or any other object. When you build a car, the constructor makes sure everything starts off correctly, like setting the car's color, model, and year.
π₯ How to Define a Constructor? π₯
In C#, a constructor has the same name as the class, and it doesn't have a return type (not even void
). You can define a constructor to take parameters so you can provide initial values when creating an object.
π₯ Example of a Constructor π₯
Letβs create a class called Car
and use a constructor to initialize its properties when we create a new car.
public class Car
{
// Properties
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
// Constructor
public Car(string make, string model, int year)
{
// Initialize the properties using parameters
Make = make;
Model = model;
Year = year;
}
}
π₯ Explanation π₯
- Constructor: The
Car
class has a constructor namedCar
. It takes three parameters:make
,model
, andyear
. When you create a newCar
object, you can pass values for these parameters. - Initialize Properties: Inside the constructor, we set the values of
Make
,Model
, andYear
using the values passed in the constructor.
π₯ Using the Constructor π₯
Now that we have a constructor in place, letβs create an object of the Car
class and pass values to the constructor.
class Program
{
static void Main()
{
// Create a Car object using the constructor
Car myCar = new Car("Toyota", "Corolla", 2020);
// Display the car details
Console.WriteLine($"Car Make: {myCar.Make}");
Console.WriteLine($"Car Model: {myCar.Model}");
Console.WriteLine($"Car Year: {myCar.Year}");
}
}
π₯ Explanation π₯
- Creating the Object: When we write
new Car("Toyota", "Corolla", 2020)
, the constructor is called, and it initializes themyCar
object with the values"Toyota"
,"Corolla"
, and2020
. - Accessing Properties: We can now access the
Make
,Model
, andYear
properties ofmyCar
just like before.
π₯ Default Constructor π₯
If you donβt define a constructor, C# provides a default constructor automatically. This constructor doesnβt take any parameters and doesnβt set any properties. Itβs used when you create an object without passing any values.
Example:
public class Car
{
// Properties
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
// Default Constructor (Automatically created by C# if no constructor is defined)
public Car()
{
Make = "Unknown";
Model = "Unknown";
Year = 0;
}
}
Now you can create a Car
without passing any values:
Car myCar = new Car();
Console.WriteLine($"Car Make: {myCar.Make}"); // Output: Unknown
Console.WriteLine($"Car Model: {myCar.Model}"); // Output: Unknown
Console.WriteLine($"Car Year: {myCar.Year}"); // Output: 0
π₯ Constructor Overloading π₯
You can also have multiple constructors with different parameters, which is called constructor overloading. This allows you to create objects in different ways depending on what data you have.
Example:
public class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
// Constructor 1: With parameters
public Car(string make, string model, int year)
{
Make = make;
Model = model;
Year = year;
}
// Constructor 2: Default values
public Car()
{
Make = "Unknown";
Model = "Unknown";
Year = 0;
}
}
π₯ Explanation of Constructor Overloading π₯
- First Constructor: Initializes the object with values passed as parameters.
- Second Constructor: Creates an object with default values if no parameters are provided.
Now, you can create a Car
object using either constructor:
Car car1 = new Car("Honda", "Civic", 2021); // Using the first constructor
Car car2 = new Car(); // Using the second constructor with default values
Console.WriteLine($"Car1 Make: {car1.Make}"); // Honda
Console.WriteLine($"Car2 Make: {car2.Make}"); // Unknown
π₯ Private Constructors π₯
Sometimes you may want to prevent the creation of an object from outside the class. You can do this by making the constructor private.
Example:
public class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
// Private Constructor
private Car()
{
Make = "Unknown";
Model = "Unknown";
Year = 0;
}
}
In this case, you cannot create a Car
object directly using new Car()
because the constructor is private. You could use a static method to create the object instead.