Interface reference in C#In this article, we will discuss the Interface reference in C# with its syntax, parameters, and examples. IntroductionAn 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. Condensed explanation of Interface Reference:
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#. Output: Area of Circle: 78.53981633974483 Area of Rectangle: 24 Explanation: 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#. Output: Woof! Meow! Explanation: 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. Conclusion: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. Next TopicQueue.Contains() Method in C# |