In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to understand C#delegation and events". In daily operation, I believe many people have doubts about how to understand C#delegation and events. The editor consulted various materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "how to understand C#delegation and events"! Next, please follow the small series to learn together!
What is C#Commission?
The concepts of C#delegate and C#event are perfectly compatible. A delegate is simply a pointer to a function, that is, it can refer to a function through the mechanism of passing an address. A delegate is a class that, when you instantiate it, provides a reference function as an argument to its constructor.
Each delegate has its own signature, for example: Delegate int SomeDelegate(string s, bool b); is a delegate declaration, in this case, the signature mentioned, that is, SomeDelegate This delegate has parameters of type string and bool, and returns an int type.
As mentioned above, when you instantiate a delegate, you provide a reference function as an argument to its constructor. Note here that the referenced function must have the same signature as the delegate.
Look at the following function:
private int SomeFunction(string str, bool bln){...}
Understanding of C#Events
We know that in C#:
Button is a class that triggers a click event when we click it.
Timer is also a class that triggers a tick event every millisecond.
There is now a Counter class that has a method CountTo(int countTo, int reachableNum) that says that a NumberReached event will be triggered when a specified time point reachableNum is reached within a specified time period (0~~countTo).
It also has one event: NumberReached, and the C#event is a variable of delegate type. If you name an event, declare it with the event keyword and the delegate type to be used, as follows:
public event NumberReachedEventHandler NumberReached;
In the declaration above, NumberReachedEventHandle is just a delegate, more precisely: NumberReachedDelegate. But Microsoft doesn't think of MouseDelegate or PaintDelegate as such, but rather MouseEventHandler or PaintEventHandler. so
NumberReachedEventHandler sounds a bit more convenient than NumberReachedDelegate, OK? Okay, let's move on, now you know that before we declare an event, we need to define a delegate in a form like this:
public delegate void NumberReachedEventHandler(object sender, NumberReachedEventArgs e);
Now declare C#delegate NumberReachedEventHandle, which has a void return value, and object, NumberReachedEventArgs two parameters. As we emphasized in section ***, when a delegate is instantiated, the function passed as an argument must also have the same signature as the delegate.
In your code, have you used PaintEventArgs or MouseEventArgs to determine where the mouse moves? Have you ever used the Graphics attribute on an object that triggered a Paint event? In fact, classes that provide data to users inherit from the System.EventArgs class, which is what we often call the event parameter class. If the event does not provide parameters, the class is not defined. In our example, we provide the expected point in time via the following class.
public class NumberReachedEventArgs : EventArgs { private int _reached; public NumberReachedEventArgs(int num) { this._ reached = num; } public int ReachedNumber { get{ return _reached; } } } At this point, the study of "how to understand C#delegation and events" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.