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 attributes and methods that a class must implement without mentioning the specifics of how they should be implemented.

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#

In C#, you can use the interface keyword and the interface's name to define an interface. As demonstrated in the example below, the interface definition may comprise methods, properties, events, and indexers −

interface  <interface_name> {
   // declare Events
   
   // declare properties
   
   // declare indexers
   
   // declare methods 
}
  • Colon Operator − the syntax to implement interface includes a colon (:) operator followed by the name of the interface you want to implement.

  • Properties − properties are the values in interface

  • Methods − methods are the functions in interface

Example

In this example, we will define an interface Shape with a method CalArea(). To calculate the area of a shape. For this purpose, we will define a class Circle that implements the Shape interface and provides an implementation for the CalArea() method used by the interface.

Algorithm

  • Step 1 − In the first step define an interface with the required methods and properties. You can include properties, methods, events, and indexers while defining the interface.

  • Step 2 − Next create a class that implements the interface.

  • Step 3 − Create a reference variable of the interface type.

  • Step 4 − Instantiate the class and assign the object to the reference variable.

  • Step 5 − Lastly use the interface reference to call the methods and properties defined in the interface.

using System;
interface Shape {
   double CalArea();
}
class Circle : Shape {
   private double radius;
   public Circle(double r) {
      radius = r;
   }
   public double GetArea() {
      return 3.14 * radius * radius;
   }
}
class Program {
   static void Main(string[] args) {
      Shape shapeRefr;
      Circle Obj = new Circle(5);
      shapeRefr = Obj;
      Console.WriteLine("Area of the circle is " + shapeRefr.CalArea());
   }
}

Output

Area of the circle is 78.5

Example

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

Algorithm

  • Step 1 − In the first step define an interface with the required 2 methods: one method to calculate marks and the other to calculate the percentage.

  • Step 2 − Next create a class that implements the interface.

  • Step 3 − Create a reference variable of the interface type.

  • Step 4 − Instantiate the class and assign the object to the reference variable.

  • Step 5 − Lastly use the interface reference to call the methods and properties defined in the interface.

using System;
interface Olevel   //create interface {
   double marks();
   double percentage();
}
class Result : Olevel  //create class {
   private double Math;
   private double Science;
   private double English;
   private double Computer;
   public Result(double math, double science, double english, double computer) {
      this.Math = math;
      this.Science = science;
      this.English = english;
      this.Computer = computer;
   }

   //create methods
   public double marks() {
      double mrks;
      mrks= Math+Science+English+Computer;
      return mrks;
   }
   public double percentage() {
      double x= Math+Science+English+Computer;
      return (x/400) * 100;
   }
}
class Program {
   static void Main(string[] args) {
      Result result = new Result(90, 95, 93, 98);

      // Create an interface reference variable and assign the instance of result class to it
      Olevel olev = result;
      Console.WriteLine("The Total marks of the student out of 400 are: " + result.marks());
      Console.WriteLine("The percentage of the student is: " + result.percentage());
   }
}

Output

The Total marks of the student out of 400 are: 376
The percentage of the student is: 94

Conclusion

Finally, interface references in C# give your code a strong mechanism. You can create code with any object that supports that interface, irrespective of its specific class.

Updated on: 25-Apr-2023

307 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements