C#  

Understanding Constructor Overloading in C# WebForms

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

  1. Class Student

    • Has three constructors with the same name (Student) but different parameter lists.

    • Each constructor initializes the object differently.

  2. Default Constructor

    • Runs when no arguments are passed.

    • Sets default values like "Unknown", 0, "Not Assigned".

  3. Parameterized Constructors

    • Overloaded constructors that accept different numbers of parameters.

    • Initialize the Student object with provided details.

  4. WebForm Logic

    • Button click event creates 3 objects using different constructors.

    • Each object’s details are displayed on the page.

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

ConceptDescription
ConstructorA special method used to initialize objects.
Constructor OverloadingMultiple constructors with different parameters.
Compile-Time PolymorphismConstructor selection is done at compile time.
EncapsulationProtects and organizes data in objects.

Real-Time Use Case

ExampleDescription
Employee RegistrationCreate an employee with or without optional details (email, department).
Product ManagementCreate a product with only a name or with a name, price, and stock.
Student EnrollmentCreate 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.