Introduction
In C#, a constructor is a special method in a class that initializes objects.
When a class has multiple constructors with different parameters, it is known as Constructor Overloading — a type of compile-time polymorphism.
This concept is commonly used in real-world applications to create objects in different ways, depending on the information available at the time of creation.
Objective
To define multiple constructors in a class.
To understand how constructor overloading works in C#.
To implement it using a real-time ASP.NET WebForms example.
C# WebForms Real-Time Example
Step 1: ASPX Page (ConstructorOverloadingDemo.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ConstructorOverloadingDemo.aspx.cs" Inherits="WebApp.ConstructorOverloadingDemo" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Constructor Overloading Example</title>
</head>
<body>
<form id="form1" runat="server">
<div style="font-family: Arial; margin: 40px;">
<h2>Constructor Overloading in C# WebForms</h2>
<asp:Button ID="btnShow" runat="server" Text="Show Constructor Output" OnClick="btnShow_Click" /><br /><br />
<asp:Label ID="lblResult" runat="server" Font-Names="Consolas" Font-Size="Large"></asp:Label>
</div>
</form>
</body>
</html>
Step 2: Code-Behind (ConstructorOverloadingDemo.aspx.cs)
using System;
namespace WebApp
{
public partial class ConstructorOverloadingDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnShow_Click(object sender, EventArgs e)
{
string result = "";
// Using default constructor
Student s1 = new Student();
result += "<b>Default Constructor:</b><br/>" + s1.DisplayDetails() + "<br/><br/>";
// Using parameterized constructor (2 parameters)
Student s2 = new Student("Sandhiya", 22);
result += "<b>Parameterized Constructor (Name, Age):</b><br/>" + s2.DisplayDetails() + "<br/><br/>";
// Using parameterized constructor (3 parameters)
Student s3 = new Student("Priya", 23, "BCA");
result += "<b>Parameterized Constructor (Name, Age, Course):</b><br/>" + s3.DisplayDetails();
lblResult.Text = result;
}
}
// Step 3: Student class with constructor overloading
public class Student
{
public string Name;
public int Age;
public string Course;
// Default constructor
public Student()
{
Name = "Unknown";
Age = 0;
Course = "Not Assigned";
}
// Parameterized constructor (2 parameters)
public Student(string name, int age)
{
Name = name;
Age = age;
Course = "Not Assigned";
}
// Parameterized constructor (3 parameters)
public Student(string name, int age, string course)
{
Name = name;
Age = age;
Course = course;
}
public string DisplayDetails()
{
return $"Name: {Name}<br/>Age: {Age}<br/>Course: {Course}";
}
}
}
Explanation
Class Student
Default Constructor
Runs when no arguments are passed.
Sets default values like "Unknown", 0, "Not Assigned".
Parameterized Constructors
WebForm Logic
Output
When you click “Show Constructor Output”, the web page displays:
Default Constructor:
Name: Unknown
Age: 0
Course: Not Assigned
Parameterized Constructor (Name, Age):
Name: Sandhiya
Age: 22
Course: Not Assigned
Parameterized Constructor (Name, Age, Course):
Name: Priya
Age: 23
Course: BCA
Key Concepts Used
| Concept | Description |
|---|
| Constructor | A special method used to initialize objects. |
| Constructor Overloading | Multiple constructors with different parameters. |
| Compile-Time Polymorphism | Constructor selection is done at compile time. |
| Encapsulation | Protects and organizes data in objects. |
Real-Time Use Case
| Example | Description |
|---|
| Employee Registration | Create an employee with or without optional details (email, department). |
| Product Management | Create a product with only name or with name, price, and stock. |
| Student Enrollment | Create a student record when only partial info is known initially. |
Conclusion
Constructor Overloading allows you to initialize objects in multiple ways, depending on the available data.
It provides flexibility, improves code readability, and is widely used in real-world enterprise applications such as employee management, inventory systems, and registration portals.