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 named Car. It takes three parameters: make, model, and year. When you create a new Car object, you can pass values for these parameters.
  • Initialize Properties: Inside the constructor, we set the values of Make, Model, and Year 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 the myCar object with the values "Toyota", "Corolla", and 2020.
  • Accessing Properties: We can now access the Make, Model, and Year properties of myCar 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.