To make code reusable in C#, use interfaces. An interface defines the properties, methods, and events that are members of the interface. An interface only contains declarations of members. Derived classes are responsible for defining members. This often helps provide a standard structure that derived classes follow.
For example, shape interface −
public interface IShape { void display(); }
Above we declared an interface Shape. You can notice it starts with a capital "I". It is a common convention for interface names to begin with "I".
We did not add access modifiers because interface members are public by default.
In practical applications, polymorphism is the key to code reuse. Interfaces are flexible because if you use an interface, you can pass anything that implements that interface through it.
The above is the detailed content of How to make C# code reusable?. For more information, please follow other related articles on the PHP Chinese website!