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

Mastering C# Interfaces: Definition, Implementation, and Practical Use Cases

C# Interfaces Explained

This guide walks you through the fundamentals of C# interfaces, shows how to create and implement them, and highlights real‑world scenarios where they shine.

What Is a C# Interface?

An interface in C# is a contract that defines a set of methods, properties, events, or indexers without providing any implementation. Unlike an abstract class, every member of an interface is implicitly abstract and public. Because of this, interfaces are ideal for defining capabilities that multiple, unrelated classes can share.

interface IPolygon
{
    void CalculateArea();
}

Implementing an Interface

To use an interface, a class must implement it by providing concrete definitions for all its members. The colon (:) syntax is used, just like inheritance.

using System;

namespace CsharpInterface
{
    interface IPolygon
    {
        void CalculateArea(int length, int breadth);
    }

    class Rectangle : IPolygon
    {
        public void CalculateArea(int length, int breadth)
        {
            int area = length * breadth;
            Console.WriteLine($"Area of Rectangle: {area}");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Rectangle rect = new Rectangle();
            rect.CalculateArea(100, 200);
        }
    }
}

Output

Area of Rectangle: 20000

Implementing Multiple Interfaces

C# allows a class to implement several interfaces simultaneously, enabling multiple inheritance of types.

using System;

namespace CsharpInterface
{
    interface IPolygon
    {
        void CalculateArea(int a, int b);
    }

    interface IColor
    {
        void GetColor();
    }

    class Rectangle : IPolygon, IColor
    {
        public void CalculateArea(int a, int b)
        {
            int area = a * b;
            Console.WriteLine($"Area of Rectangle: {area}");
        }

        public void GetColor()
        {
            Console.WriteLine("Red Rectangle");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Rectangle r = new Rectangle();
            r.CalculateArea(100, 200);
            r.GetColor();
        }
    }
}

Output

Area of Rectangle: 20000
Red Rectangle

Using an Interface Reference

Even though you cannot instantiate an interface directly, you can declare a variable of that interface type and assign it an instance of any implementing class. This is a powerful tool for achieving polymorphism.

using System;

namespace CsharpInterface
{
    interface IPolygon
    {
        void CalculateArea(int length, int breadth);
    }

    class Rectangle : IPolygon
    {
        public void CalculateArea(int length, int breadth)
        {
            int area = length * breadth;
            Console.WriteLine($"Area of Rectangle: {area}");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IPolygon poly = new Rectangle();
            poly.CalculateArea(100, 200);
        }
    }
}

Output

Area of Rectangle: 20000

Practical Example: Polymorphic Shape Calculations

Below we define an IPolygon interface and two concrete shapes that implement it. Each shape calculates its area independently.

using System;

namespace CsharpInterface
{
    interface IPolygon
    {
        void CalculateArea();
    }

    class Rectangle : IPolygon
    {
        public void CalculateArea()
        {
            int length = 30;
            int breadth = 90;
            int area = length * breadth;
            Console.WriteLine($"Area of Rectangle: {area}");
        }
    }

    class Square : IPolygon
    {
        public void CalculateArea()
        {
            int side = 30;
            int area = side * side;
            Console.WriteLine($"Area of Square: {area}");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Rectangle rect = new Rectangle();
            rect.CalculateArea();

            Square sq = new Square();
            sq.CalculateArea();
        }
    }
}

Output

Area of Rectangle: 2700
Area of Square: 900

Why Use C# Interfaces?


C Language

  1. Mastering Java Interfaces: Concepts, Implementation, and Best Practices
  2. Java Collections Framework: Core Interfaces, Implementations, and Practical Usage
  3. Java Collection Interface: Core Concepts & Essential Methods
  4. Mastering Java’s Queue Interface: Methods, Implementations, and Practical Use
  5. Mastering Java's Deque Interface: Features, Methods, and Practical Examples
  6. Java Map Interface – Comprehensive Guide to Map, Its Implementations, and Key Methods
  7. Java SortedMap Interface: Overview, Methods, and TreeMap Implementation
  8. Mastering Java NavigableMap: Features, Methods, and TreeMap Implementation
  9. Mastering Java’s Set Interface: Concepts, Methods, and Practical Examples
  10. Mastering C# Interfaces: Design Contracts for Robust Object-Oriented Programming