Home > Backend Development > C#.Net Tutorial > How to make C# code reusable?

How to make C# code reusable?

王林
Release: 2023-09-02 09:37:02
forward
796 people have browsed it

如何使 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();
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template