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 use the c # event

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

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces "how to use the c # event" in detail, the content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use the c # event" can help you solve your doubts. Let's learn new knowledge.

Events are based on delegates and can provide a publish / subscribe mechanism for any delegate type.

Use the event keyword to define a delegate type as an event.

Let's introduce the event through an example:

/ / event publishing class public class PublishEvent {public delegate string Display (string str); public event Display DisplayEvent; / / client code triggers event public void Shows (string str) {if (DisplayEvent! = null) {DisplayEvent (str) by calling this method } / / event listening class, which subscribes to event public class Listen1 {public string MakeAlert (string str) {Console.WriteLine (str + "Listen1"); return str + "Listen1" }} public class Listen2 {public string ShowMsg (string str) {Console.WriteLine (str + "Listen2"); return str + "Listen2";}}

Client code:

Class Program {static void Main () {PublishEvent pe = new PublishEvent (); Listen1 L1 = new Listen1 (); Listen2 L2 = new Listen2 (); / / variables L1 and L2 subscribe to the event pe.DisplayEvent + = L1.MakeAlert; pe.DisplayEvent + = l2.ShowMsg / / trigger event pe.Shows ("event"); Console.ReadKey ();}}

An event is a special delegate, which is a special delegate for the event-driven model. You can call the delegate directly in the client code to fire the function that the delegate points to, but the event cannot, and the event can only be triggered by the service code itself. In other words, delegating in your code, you can not only arrange who is its calling function, but also call it directly, while events cannot be called directly, but can only be triggered by certain actions. Apart from this, the event has all the functions of the delegate, including the multicast feature. That is, an event can have multiple event handlers, and a delegate can also be a multicast delegate.

Events are encapsulated delegate instances; delegates are types, events are instances!

Delegates that come with EventHandler.NET are also used to define events.

After reading this, the article "how to use the c # event" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about the relevant articles, 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