Javatpoint Logo
Javatpoint Logo

Interface reference in C#

In this article, we will discuss the Interface reference in C# with its syntax, parameters, and examples.


An interface connection in C# functions similarly to a parameter that was previously designated to be of an interface type. In C#, an interface is a container that specifies a collection of methods, attributes, occurrences, or indexing that a particular class must offer. It functions like a binding agreement that classes have to follow. By attaining polymorphic behavior and abstracting away particular implementations, interface references help to improve the flexibility and modularity that accompany the code.

Interface reference in C#

Condensed explanation of Interface Reference:

  • Declaration: Interface references are specified using the interface type as the data type to provide a degree of transparency without naming a specific implementation.
  • Assignment: In order to enable dynamic binding at the time of execution, these references can be assigned to objects of classes that implement the interface.
  • Polymorphism: Interface references supply polymorphic behavior, which implies that independent of the individual implementation, anyone may use the interface referenced to invoke methods or access attributes described in the interface on objects.
  • Dynamic Binding: At runtime, an interface reference invokes a method or property that is dynamically bound to the implementation that is given by the object that the reference points correspond to class. Developers can build code that is less tightly connected to particular implementations, more flexible, and easier to maintain by interface references. This method provides the capacity for adaptability and extensibility of the codebase by allowing the replacement of various implementations of the same interface while maintaining loose coupling between components of an application.
  • Function: In object-oriented programming, interface references in C# are used to enable polymorphism, abstraction, and decoupling. Following is a summary of their primary roles:
    1. Polymorphism: When two components of distinct categories implement the same interface, they can be treated interchangeably due to their interface references, which provide polymorphic behavior. By enabling methods to be built to accept interface references as opposed to specific class types, this encourages code flexibility and reusability. Based on the actual object type addressed, the appropriate method implementation is dynamically bound during gameplay.
    2. Abstraction: By providing a set of rules that classes must follow to establish an interface, interface references help to advance encapsulation. It frees up developers from focusing on the functionality (the interface) that an object ought to supply rather than the technical details of implementation.
    3. Decoupling: By lowering reliance on particular implementations, interface references assist in the decoupling of components of the system. The concrete classes that implement those interfaces are not linked to code written to interface variables. It promotes maintainability and scalability by making it simpler to modify or expand a component's implementation without affecting other areas of the codebase.

Overall, by facilitating polymorphic behavior, abstraction, and decoupling between components, interface references in C# help contribute to flexibility, modularity, and maintainability in oriented towards object architecture.

Example 1:

Let us take an example to illustrate the Interface Reference in C#.


Area of Circle: 78.53981633974483
Area of Rectangle: 24


Interface references and polymorphism are concepts that are shown in the accompanying C# code. Initially, a specified interface called IShape is established, containing the function CalculateArea () for figuring approximately the area of different forms. The Circle and Rectangle classes implement this interface, each one of which provides a different implementation of the CalculateArea () function. Circle and Rectangle instances are produced in the Program class's Main function. Polymorphic functionality is demonstrated by the declaration and assignment of interface elements shape1 and shape2 of type IShape to these instances. By using these interface references, the CalculateArea () function may be called without having to know the particular object type to use because it will constantly connect to the right functionality depending on the object it symbolizes.

Example 2:

Let us take another example to illustrate the Interface Reference in C#.




In this example, two classes that implement the IAnimal interface are defined: Dog and Cat. The Speak() function has concrete implementations in these classes.

The Speak() function is implemented differently in each class to reflect the diverse noises made by cats and dogs. When Speak() is invoked, the member of the Dog class prints "Woof!" and the Cat class prints "Meow!".

In the Main() Function:

We define an interface reference animal of type IAnimal in the Main function. We can refer to objects of many different classes that implement the same interface through the use of interface references.

We illustrate polymorphism by designating instances of both Dog and Cat to the animal reference. Thus, an object that is either a dog or a cat may be referenced by the animal reference.

We may call the Speak() function consistently using the animal reference, even though the underlying types (Dog and Cat) differ. The above is a crucial aspect of polymorphism in that it enables us to handle objects of various kinds consistent conforming to the common characteristics that the interface defines.


A variable that may store a reference to any object that implements a particular interface is commonly referred to as an interface connection in C#. In C#, an interface defines a contract that outlines the methods, properties, occurrences, and indexers that implementers of a class should supply. Polymorphic behavior is made possible by an interface reference, which lets programs interact with objects by using their shared behavior as described by the interface instead of their distinct types.

In object-oriented programming, interface references are essential to achieve abstraction, polymorphism, and a lack of coupling. They make it attainable for programmers to design more adaptable, reusable, and maintained code. Applications become more flexible because they are programmed to interfaces rather than real-world implementations. After all, alternative implementations may be easily changed out as long as they follow the interface agreement.

When two objects of different kinds implement the same interface, code may treat them equally due to interface references. It makes it easier to write modular, testable, and extendable software components and encourages the reuse of code. In C# programming, interface referencing are frequently employed, particularly in circumstances in which flexibility and abstraction are crucial, including dependency injection, architectural patterns like the Strategy pattern, and putting APIs or frameworks into practice.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA