How do you make code reusable in C#?

CsharpProgrammingServer Side Programming

To make code reusable in C#, use Interfaces. Interfaces define properties, methods, and events, which are the members of the interface. Interfaces contain only the declaration of the members. It is the responsibility of the deriving class to define the members. It often helps in providing a standard structure that the deriving classes would follow.

For example, Shape Interface −

public interface IShape {
   void display();
}

Above we have declared an Interface Shape. You can notice that it begins with a capital “I”. It is a common convention that interfaces names begin with “I”.

We have not added an access specifier above since interface members are public by default.

In real applications, Polymorphism is key for code reuse. Interfaces are flexible because if you use interfaces, then with its help you can pass anything that implements that Interface.

raja
Published on 20-Aug-2018 13:30:54
Advertisements