Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of interfaces in C #

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail what is the use of the interface in C#, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

By the way, after playing the blog for a period of time, what I have been publishing is the relatively basic knowledge of C#. After the basic publication is over, I will continue to publish something about the project.

The concept and declaration of interface

An interface is a protocol used to define a program that describes a set of related behaviors that can belong to any class or structure. An interface can have methods, properties, events, and indexers, or any combination of these four members, but cannot contain fields.

So what are the characteristics of the interface?

An interface is similar to an abstract base class: any non-abstract type that inherits an interface must implement all members of the interface (note: if class An inherits interface B, then A must implement the properties, methods, etc.) defined in B.

Interfaces cannot be instantiated directly

Interfaces can contain events, indexers, methods, and properties

The interface does not contain the implementation of the method

Classes and interfaces can inherit from multiple interfaces

The interface itself can inherit multiple interfaces

When declaring an interface, except for Interface and interface name, everything else is optional. An interface can also be implemented using modifiers such as new, public, protected, intenal, and private, but interface members must be public.

Implementation and inheritance of interface

When you declare a class that implements an interface, you need to include the name of the interface that the class implements in the base class list.

All methods in the inherited interface must be implemented in a derived class. After OK, single inheritance and multiple inheritance are all understood, is there nothing else to know? Imagine that if you inherit from interfaces B and C in a class An and include members with the same signature in B and C, implementing that member in the class will cause both interfaces to use that member as their implementation

The Add methods in the two interfaces are implemented by two methods to display the interface members. After instantiating different interfaces, the corresponding methods are called to output the results.

On the interface in C# what is useful to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report