Home  >  Article  >  Backend Development  >  How to make C# code reusable?

How to make C# code reusable?

王林
王林forward
2023-09-02 09:37:02717browse

如何使 C# 代码可重用?

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete