Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Constructor overloading in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Constructor overloading
Create object
Check constructor parameters
No params
Call matching constructor
Initialize object fields
Object ready to use
When creating an object, the program chooses the constructor that matches the given parameters to initialize the object properly.
Execution Sample
C Sharp (C#)
class Box {
  int length, width;
  public Box() { length = 1; width = 1; }
  public Box(int l) { length = l; width = 1; }
  public Box(int l, int w) { length = l; width = w; }
}
Defines a Box class with three constructors: no parameters, one parameter, and two parameters.
Execution Table
StepConstructor CalledParametersField lengthField widthAction
1Box()none11Default constructor sets length=1, width=1
2Box(int l)551Constructor with one parameter sets length=5, width=1
3Box(int l, int w)5, 10510Constructor with two parameters sets length=5, width=10
4Exit---All constructors executed as per parameters
💡 Execution stops after all constructor calls are demonstrated.
Variable Tracker
VariableStartAfter 1After 2After 3Final
lengthundefined1555
widthundefined111010
Key Moments - 3 Insights
Why does the constructor with one parameter set width to 1?
Because in the constructor Box(int l), width is explicitly set to 1, as shown in step 2 of the execution_table.
What happens if no constructor matches the parameters?
The program will give an error. Here, each constructor matches the parameters given, so no error occurs (see step 4 exit note).
How does the program decide which constructor to call?
It looks at the number and type of parameters when creating the object and calls the matching constructor, as shown in the concept_flow and execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of width after step 2?
A1
B5
C10
Dundefined
💡 Hint
Check the 'Field width' column in row 2 of the execution_table.
At which step does the constructor set length to 5 and width to 10?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Constructor Called' and 'Field width' columns in the execution_table.
If we create a Box object with no parameters, what will length be according to variable_tracker?
Aundefined
B1
C5
D10
💡 Hint
Check the 'After 1' column for 'length' in variable_tracker.
Concept Snapshot
Constructor overloading allows multiple constructors with different parameters.
The program calls the constructor matching the given arguments.
Each constructor initializes object fields differently.
This helps create objects in flexible ways.
Syntax: multiple constructors with different parameter lists.
Full Transcript
Constructor overloading means a class can have several constructors with different parameters. When you create an object, the program picks the constructor that matches the parameters you give. For example, a Box class can have a constructor with no parameters that sets default size, one parameter to set length, and two parameters to set length and width. The execution table shows how each constructor sets the fields length and width. The variable tracker shows how these fields change after each constructor call. This helps beginners understand how the program chooses and runs the right constructor to build the object.

Practice

(1/5)
1. What does constructor overloading in C# allow you to do?
easy
A. Override methods with the same name
B. Create multiple constructors with different parameter lists in the same class
C. Use constructors without parameters only
D. Create only one constructor per class

Solution

  1. Step 1: Understand constructor overloading concept

    Constructor overloading means having more than one constructor in a class, each with a different set of parameters.
  2. Step 2: Identify what overloading allows

    This allows creating objects in different ways depending on the parameters passed.
  3. Final Answer:

    Create multiple constructors with different parameter lists in the same class -> Option B
  4. Quick Check:

    Constructor overloading = multiple constructors with different parameters [OK]
Hint: Multiple constructors differ by parameter list only [OK]
Common Mistakes:
  • Thinking only one constructor is allowed
  • Confusing overloading with overriding
  • Believing constructors must have no parameters
2. Which of the following is a correct constructor overloading syntax in C#?
easy
A. public class Car { public Car() {} public Car(string model) {} }
B. public class Car { public void Car() {} public void Car(string model) {} }
C. public class Car { public Car() {} public Car() {} }
D. public class Car { Car() {} Car() {} }

Solution

  1. Step 1: Check constructor syntax

    Constructors must have the same name as the class and no return type.
  2. Step 2: Identify correct overloading

    public class Car { public Car() {} public Car(string model) {} } has two constructors with different parameters and correct syntax.
  3. Final Answer:

    public class Car { public Car() {} public Car(string model) {} } -> Option A
  4. Quick Check:

    Constructor syntax correct and overloaded by parameters [OK]
Hint: Constructors have no return type and match class name [OK]
Common Mistakes:
  • Adding return type to constructors
  • Defining multiple constructors with same parameters
  • Omitting access modifier (not mandatory but common style)
3. What will be the output of this code?
class Box {
  public int length;
  public Box() { length = 5; }
  public Box(int l) { length = l; }
}
class Program {
  static void Main() {
    Box b1 = new Box();
    Box b2 = new Box(10);
    Console.WriteLine(b1.length + ", " + b2.length);
  }
}
medium
A. 5, 10
B. 0, 10
C. 5, 5
D. 10, 10

Solution

  1. Step 1: Analyze constructors called

    b1 uses the parameterless constructor setting length = 5; b2 uses the constructor with int parameter setting length = 10.
  2. Step 2: Determine printed values

    Console.WriteLine prints b1.length (5) and b2.length (10) separated by a comma.
  3. Final Answer:

    5, 10 -> Option A
  4. Quick Check:

    Default constructor sets 5, parameterized sets 10 [OK]
Hint: Check which constructor is called for each object [OK]
Common Mistakes:
  • Assuming default int value 0 instead of assigned 5
  • Confusing which constructor runs for each object
  • Mixing up output order
4. Identify the error in this constructor overloading code:
class Person {
  public string name;
  public Person(string n) { name = n; }
  public Person(string n) { name = n.ToUpper(); }
}
medium
A. Constructor name does not match class name
B. Missing return type in constructors
C. Duplicate constructor with same parameter list
D. Cannot assign string to name

Solution

  1. Step 1: Check constructor parameter lists

    Both constructors have the same parameter type and count (string n), causing duplication.
  2. Step 2: Understand overloading rules

    Constructors must differ by parameter types or count to overload; identical signatures cause error.
  3. Final Answer:

    Duplicate constructor with same parameter list -> Option C
  4. Quick Check:

    Same parameters = duplicate constructor error [OK]
Hint: Constructor signatures must differ by parameters [OK]
Common Mistakes:
  • Thinking constructors can differ by body only
  • Adding return type mistakenly
  • Ignoring parameter list uniqueness
5. You want to create a class Rectangle with overloaded constructors:
- One constructor takes no parameters and sets width and height to 1.
- Another takes one parameter and sets both width and height to that value.
- Another takes two parameters to set width and height separately.
Which of these constructor definitions correctly implements this?
hard
A. public Rectangle() { width = 1; height = 1; } public Rectangle(int w, int h) { width = w; height = h; } public Rectangle(int w, int h) { width = w; height = h; }
B. public Rectangle() { width = 1; height = 1; } public Rectangle(int size) { width = size; height = size; } public Rectangle(int size) { width = size; height = size; }
C. public Rectangle(int w, int h) { width = w; height = h; } public Rectangle() { width = 1; height = 1; } public Rectangle(int size) { width = size; }
D. public Rectangle() { width = 1; height = 1; } public Rectangle(int size) { width = size; height = size; } public Rectangle(int w, int h) { width = w; height = h; }

Solution

  1. Step 1: Check parameter lists for uniqueness

    public Rectangle() { width = 1; height = 1; } public Rectangle(int size) { width = size; height = size; } public Rectangle(int w, int h) { width = w; height = h; } has three constructors with distinct parameter lists: no parameters, one int, and two ints.
  2. Step 2: Verify each constructor sets values correctly

    Each constructor sets width and height as required: default 1, same size, or separate sizes.
  3. Final Answer:

    public Rectangle() { width = 1; height = 1; } public Rectangle(int size) { width = size; height = size; } public Rectangle(int w, int h) { width = w; height = h; } -> Option D
  4. Quick Check:

    Distinct parameter lists and correct assignments [OK]
Hint: Each constructor must have unique parameter count or types [OK]
Common Mistakes:
  • Defining two constructors with same parameter types
  • Mixing order of constructors causing confusion
  • Not setting default values in parameterless constructor