Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering Constructor Overloading in C#

Mastering Constructor Overloading in C#

Discover how to design flexible classes in C# by overloading constructors—varying parameter counts, types, and orders—complete with practical examples.

Constructor overloading in C# mirrors method overloading: a class can declare multiple constructors that share the same name but differ in signature. The compiler selects the appropriate constructor based on the arguments supplied when an object is instantiated.

Before diving into overloading, ensure you understand basic C# constructors.


1. Varying the Number of Parameters

You can overload a constructor by providing different numbers of arguments.

class Car
{
    // No-parameter constructor
    Car()
    {
        ...
    }

    // One-parameter constructor
    Car(string brand)
    {
        ...
    }

    // Two-parameter constructor
    Car(string brand, int price)
    {
        ...
    }
}

In this Car class, the three constructors coexist because each signature is distinct. The compiler chooses the matching constructor at runtime based on the argument count.

Example: Overloading by Parameter Count

using System;

namespace ConstructorOverload
{
    class Car
    {
        // No-parameter constructor
        Car()
        {
            Console.WriteLine("Car constructor");
        }

        // One-parameter constructor
        Car(string brand)
        {
            Console.WriteLine("Car constructor with one parameter");
            Console.WriteLine("Brand: " + brand);
        }

        static void Main(string[] args)
        {
            // Calls no-parameter constructor
            Car car = new Car();
            Console.WriteLine();

            // Calls one-parameter constructor
            Car car2 = new Car("Bugatti");
            Console.ReadLine();
        }
    }
}

Output

Car constructor

Car constructor with one parameter
Brand: Bugatti

The compiler invokes the appropriate constructor based on the arguments provided.


2. Differing Parameter Types

Constructors can also be overloaded when they accept the same number of parameters but of different types.

class Car
{
    Car(string brand)
    {
        ...
    }

    Car(int price)
    {
        ...
    }
}

Here, both constructors accept a single argument, yet the types—string versus int—differentiate them.

Example: Overloading by Parameter Type

using System;

namespace ConstructorOverload
{
    class Car
    {
        // Constructor with a string
        Car(string brand)
        {
            Console.WriteLine("Brand: " + brand);
        }

        // Constructor with an int
        Car(int price)
        {
            Console.WriteLine("Price: " + price);
        }

        static void Main(string[] args)
        {
            Car car = new Car("Lamborghini");
            Console.WriteLine();

            Car car2 = new Car(50000);
            Console.ReadLine();
        }
    }
}

Output

Brand: Lamborghini

Price: 50000

3. Changing Parameter Order

When constructors have the same number and types of parameters, you can still overload by rearranging their order.

class Car
{
    Car(string brand, int price)
    {
        ...
    }

    Car(int speed, string color)
    {
        ...
    }
}

The signatures differ because the sequence of types is distinct.

Example: Overloading by Parameter Order

using System;

namespace ConstructorOverload
{
    class Car
    {
        // String first, then int
        Car(string brand, int price)
        {
            Console.WriteLine("Brand: " + brand);
            Console.WriteLine("Price: " + price);
        }

        // Int first, then string
        Car(int speed, string color)
        {
            Console.WriteLine("Speed: " + speed + " km/hr");
            Console.WriteLine("Color: " + color);
        }

        static void Main(string[] args)
        {
            Car car = new Car("Bugatti", 50000);
            Console.WriteLine();

            Car car2 = new Car(60, "Red");
            Console.ReadLine();
        }
    }
}

Output

Brand: Bugatti
Price: 50000

Speed: 60 km/hr
Color: Red

C Language

  1. C# Constructors: Types, Examples, and Advanced Patterns
  2. Understanding C# Nested Classes: Definition, Usage, and Inheritance
  3. Mastering Method Overloading in C#: Concepts, Examples, and Best Practices
  4. C++ Function Overloading: A Practical Guide
  5. Understanding C++ Constructors: Default, Parameterized, and Copy Explained
  6. Master C++ Operator Overloading: Practical Examples & Best Practices
  7. Mastering Operator Overloading in Python: A Practical Guide
  8. Master Java Constructors: Types, Usage, and Practical Examples
  9. Constructor Overloading in Java – Explained with Practical Code Examples
  10. Mastering Operator Overloading in C# for Custom Types