How to use Interface References in C#?

C# is an object-oriented programming language that offers a unique feature known as interfaces. They enable you to declare a collection of methods and properties that a class must implement without specifying the implementation details.

The ability to write code that is independent of a class's implementation details is one of the main benefits of interfaces. Each object of any class that implements the interface can be referred to using an interface reference.

As a result, it is simpler to switch between different class implementations without having to modify the code that utilizes the class.

Syntax for Defining an Interface

In C#, you can use the interface keyword and the interface's name to define an interface. The interface definition may comprise methods, properties, events, and indexers

interface <interface_name> {
   // declare Events
   
   // declare properties
   
   // declare indexers
   
   // declare methods 
}

To implement an interface, use the colon operator followed by the interface name

class ClassName : InterfaceName {
   // implement interface members
}

Interface Reference Flow IShape interface implements Circle class reference obj instance CalArea() CalArea() { ... } IShape ref IShape shapeRef = new Circle(); shapeRef.CalArea(); // calls Circle's implementation

Using Interface References for Shape Calculation

Example

In this example, we will define an interface IShape with a method CalArea(). We will create a Circle class that implements the interface and use an interface reference to call the method

using System;

interface IShape {
   double CalArea();
}

class Circle : IShape {
   private double radius;
   
   public Circle(double r) {
      radius = r;
   }
   
   public double CalArea() {
      return 3.14 * radius * radius;
   }
}

class Program {
   static void Main(string[] args) {
      // Create interface reference
      IShape shapeRef;
      
      // Instantiate Circle class
      Circle circleObj = new Circle(5);
      
      // Assign object to interface reference
      shapeRef = circleObj;
      
      // Call method through interface reference
      Console.WriteLine("Area of the circle is " + shapeRef.CalArea());
   }
}

The output of the above code is

Area of the circle is 78.5

Using Interface References for Student Result System

Example

In this example, we will calculate marks of 4 subjects of a student and the percentage of total marks. We will use an interface with 2 methods

using System;

interface IResult {
   double CalculateMarks();
   double CalculatePercentage();
}

class StudentResult : IResult {
   private double math;
   private double science;
   private double english;
   private double computer;
   
   public StudentResult(double math, double science, double english, double computer) {
      this.math = math;
      this.science = science;
      this.english = english;
      this.computer = computer;
   }

   public double CalculateMarks() {
      return math + science + english + computer;
   }
   
   public double CalculatePercentage() {
      double totalMarks = math + science + english + computer;
      return (totalMarks / 400) * 100;
   }
}

class Program {
   static void Main(string[] args) {
      // Create instance of StudentResult class
      StudentResult result = new StudentResult(90, 95, 93, 98);

      // Create interface reference and assign the instance
      IResult resultRef = result;
      
      // Use interface reference to call methods
      Console.WriteLine("Total marks out of 400: " + resultRef.CalculateMarks());
      Console.WriteLine("Percentage: " + resultRef.CalculatePercentage() + "%");
   }
}

The output of the above code is

Total marks out of 400: 376
Percentage: 94%

Benefits of Interface References

Benefit Description
Polymorphism Different classes can be treated uniformly through the same interface
Flexibility Easy to switch between different implementations without code changes
Testability Enables mock objects and unit testing
Loose Coupling Code depends on abstractions, not concrete implementations

Conclusion

Interface references in C# provide a powerful mechanism for writing flexible and maintainable code. You can create code that works with any object that implements a specific interface, regardless of its concrete class, enabling polymorphism and loose coupling in your applications.

Updated on: 2026-03-17T07:04:36+05:30

933 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements