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

How to solve some problems of C# Interface members

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

Share

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

Editor to share with you how to solve some of the problems of C# interface members, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article. Let's learn about it!

Why the members of C# interface didn't delegate

We all know that C# interfaces can contain events. In fact, when we see events, it is easy to think of delegates, which are the basis of events. Programmers who are not very clear about delegates and events will not understand why C# interfaces can contain events but not delegates. To put it simply, a delegate is also a type, and the delegate keyword introduces a new type, so a C# interface cannot contain a delegate and treat it as a member, while the event keyword introduces a new member, so events can be attributed to the interface. To understand this, let's start with the mission of the C # interface, which is a contract that regulates the behavior of the interface implementer, not something. It is very simple, for example, "Party member" is an interface, it must have an action to "serve the people", and "so-and-so Party member" has realized the interface of "Party member", then "so-and-so Party member" must also "serve the people". As to whether you "so-and-so Party members" must use "computers" and "children". Then there will certainly be no rules in the interface of "Party members". This is the purpose of the interface, standardizing some of the behavior of the implementer. So the members of the C# interface are all methods, and there will be nothing else. Programmers with a little common sense of c # understand that the properties in c # are actually two methods, a Set method and a Get method, and the same events and indexers are also methods. See the following interface:

PublicinterfaceIDrawingObject {eventEventHandlerOnDraw; stringName {get; set;} intthis [intindex] {get; set;} voidSetValue ();}

This interface contains all the members, events, properties, indexers, and methods that can be accepted by the C# interface. After compiling the interface, let's take a look at it with the MSIL Disassembler tool:

Now we all understand that the attribute Name corresponds to the two methods Get_Name () and Set_Name (), the event OnDraw corresponds to the two methods add_OnDraw () and remove_OnDraw (), and the indexer corresponds to the two methods get_Item () and set_Item (). Take a look at the following definitions of delegates and classes:

PublicdelegatevoidTestEventDelegate (objectsender,System.EventArgse); classTestClass {publicvoidSetValue () {}}

You see, there is no difference between defining a delegate and defining a class, both defining a new type. So the C # interface cannot be delegated unless Microsoft tells us that classes can be defined in the C # interface.

The above is all the contents of this article entitled "how to solve some problems of C# interface members". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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