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

Analysis of examples of C# event handling

2025-03-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "C# event handling example Analysis". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

C# event handling

When you write a class, it is sometimes necessary to let the client of the class know what has happened. If you are a programmer with years of programming experience, there seem to be many solutions, including function pointers for callbacks and event reception (event sinks) for ActiveX controls. Now you will learn another way to associate client code with class notifications-using events.

Events can be declared either as class domain members (member variables) or as properties. The commonness of the two is that the type of event must be the representative element, and the function pointer prototype and the representative element of C # have the same meaning.

Each event can be occupied by 0 or more customers, and the customer can associate or cancel the event at any time. You can define representative meta statically or by instance method, which is very popular with C++ programmers.

Now that I have mentioned all the functions of the event and the corresponding representative elements, take a look at the example in listing 5.11. It vividly embodies the theory.

The following code implements C# event handling in the class:

Using System; / / forward declare public delegate void EventHandler (string strText); class EventSource {public event EventHandler TextOut; public void TriggerEvent () {if (null! = TextOut) TextOut ("Event triggered");}} class TestApp {public static void Main () {EventSource evsrc = new EventSource (); evsrc.TextOut + = new EventHandler (CatchEvent); evsrc.TriggerEvent (); evsrc.TextOut-= new EventHandler (CatchEvent); evsrc.TriggerEvent (); TestApp theApp = new TestApp (); evsrc.TextOut + = new EventHandler (theApp.InstanceCatch); evsrc.TriggerEvent ();} public static void CatchEvent (string strText) {Console.WriteLine (strText) } public void InstanceCatch (string strText) {Console.WriteLine ("Instance" + strText);}} "C# event handling example Analysis" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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