C#  

Understanding Interface Implementation in C# WebForms

Introduction

In C#, an interface defines a contract — a set of methods and properties that a class must implement.
Interfaces help achieve abstraction, loose coupling, and code reusability, which are essential in large applications.

In this example, we’ll create an interface IEmployee and a class Manager that implements it.
Then we’ll display the manager’s details on a WebForm.

Objective

  • To define and implement an interface in C#.

  • To understand how multiple classes can implement the same interface differently.

  • To use an interface in a real-time ASP.NET WebForms example.

C# WebForms Real-Time Example

Step 1: ASPX Page (InterfaceDemo.aspx)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="InterfaceDemo.aspx.cs" Inherits="WebApp.InterfaceDemo" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Interface Implementation Example</title>
</head>
<body>
    <form id="form1" runat="server">
        <div style="font-family: Arial; margin: 40px;">
            <h2>Interface Implementation Example in C# WebForms</h2>

            <asp:Button ID="btnShow" runat="server" Text="Show Employee Details" 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 (InterfaceDemo.aspx.cs)

using System;

namespace WebApp
{
    public partial class InterfaceDemo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            IEmployee emp = new Manager();
            emp.SetDetails(101, "Sandhiya", 55000);
            lblResult.Text = emp.GetDetails();
        }
    }

    // Step 3: Interface Definition
    public interface IEmployee
    {
        void SetDetails(int id, string name, double salary);
        string GetDetails();
    }

    // Step 4: Class implementing the interface
    public class Manager : IEmployee
    {
        private int EmpId;
        private string Name;
        private double Salary;

        public void SetDetails(int id, string name, double salary)
        {
            EmpId = id;
            Name = name;
            Salary = salary;
        }

        public string GetDetails()
        {
            return $"<b>Employee Details</b><br/>" +
                   $"Employee ID: {EmpId}<br/>" +
                   $"Name: {Name}<br/>" +
                   $"Salary: ₹{Salary}";
        }
    }
}

Explanation

  1. IEmployee Interface

    • Declares two methods:

      • SetDetails(int id, string name, double salary)

      • GetDetails()

    • No implementation — just method signatures (blueprint).

  2. Manager Class

    • Implements the IEmployee interface using the : IEmployee syntax.

    • Provides method bodies for the declared interface methods.

    • Uses private fields to store and return employee details.

  3. WebForm Page

    • On button click, an IEmployee reference is created and assigned a Manager object.

    • Methods are called via the interface reference, demonstrating abstraction.

Output

After clicking “Show Employee Details”, the webpage displays:

Employee Details
Employee ID: 101
Name: Sandhiya
Salary: ₹55000

Key Concepts Used

ConceptDescription
InterfaceA blueprint for classes defining what methods must be implemented.
ImplementationA class must provide definitions for all interface methods.
AbstractionHides implementation details; only method names are visible.
PolymorphismAn interface can refer to multiple class types implementing it.

Real-Time Use Case

Use CaseExample
Payment SystemsIPayment interface → CreditCardPayment, UPIPayment, NetBankingPayment
Employee ManagementIEmployee interface → Manager, Developer, HR
Data Access LayerIRepository interface → SqlRepository, OracleRepository

Conclusion

This example demonstrates Interface Implementation in C# WebForms, showing how interfaces help build scalable, modular, and testable systems.
Using interfaces, we can ensure a consistent structure across multiple classes while maintaining flexibility in implementation.