In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 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# delegate, time and Lambda expression". In daily operation, I believe many people have doubts about how to understand C# delegate, time and Lambda expression. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to understand C# delegate, time and Lambda expression". Next, please follow the editor to study!
The delegate in. Net is indeed very similar to the function pointer of Cstroke Candle +. It is a value type that wraps a reference to a method. Its function is also to be able to pass methods as parameters as variables. A typical application of a delegate is the event handling method of a control. Obviously, a control has no way to know what to do when a particular event occurs at design time, so you need to pass the method to the control as a parameter. Delegates are also heavily used in LINQ.
Declare a delegate to use the delegate keyword, as follows:
Delegate int Echo (string message); this code declares a delegate type, and an instance of this delegate type can accept a function with an argument of string and an int value. This method can be an object's method, a static method, or an anonymous method, as long as the signature and return value of the method are consistent with the declaration. This is very similar to C's function pointer, but the function pointer only contains the function entry address, and the delegate is a type, which is more powerful than the function pointer.
One of the points is that when the method is an instance method, this method can obtain the values of other variables of the object, which is described in detail in the second article at the beginning of the article. The second point is that delegates support multicast, that is, a series of methods can be executed in turn. For example:
The following is a code snippet:
Static int EchoOriginal (string message) {Console.WriteLine (message); return 1;} static int EchoReverse (string message) {StringBuilder sb=new StringBuilder (); for (int imessages message.Lengthmae 1) sb.Append (message[ I]); Console.WriteLine (sb.ToString ()); return-1;} static void Main (string [] args) {Echo eo = EchoOriginal; Echo er = EchoReverse; Echo all = eo + er; eo ("Hello world") Int i=all ("Hello Delegate"); Console.WriteLine (I);}
We define two methods, both of which conform to the declaration of Echo. The all instance of * Echo can accept two delegates. When calling all, eo,er will be phished once, and the return value is the return value of * a delegate. The output of the program is:
Hello world
Hello Delegat
EtageleD olleH
-1
In fact, the method does not need to be exactly the same as the signature of the delegate declaration type. Net allows the return value of the method to be the type inherited from the declared return value, and the parameter type of the method is the parent type of the declared parameter. This is Covariance and Contravariance in Delegates.
The event mechanism of .NET is based on delegation. The event mechanism consists of two parts, one is the event publisher and the other is the event responder. The implementation principle is that the event publisher declares a delegate object, the event responder mounts the specific processing method to that delegate, and the event publisher invokes the delegate when needed, so that the responder's code will be executed. In fact, .NET does the same thing. The event keyword of C # does little work, including generating a private delegate for the class. The delegate supported by event is a limited delegate, and its return value must be void, with two parameters, * the event occurrence, and the second parameter the parameter to be carried by the event. The simplest event handling delegate. Net has been declared:
The following is a code snippet:
Public delegate void EventHandler (Object sender, EventArgs e)
The basic way to declare an event is the event delegate type event name; for example, there is a class that triggers an event whenever an odd number is found. Our program outputs a prompt on the screen when it receives this event. The code for the class can be implemented as follows:
The following is a code snippet:
Public class OddFinder {public event EventHandler FindOdd; public void Find (int from, int to) {for (int I = from; i expression is a list of parameters on the left and method body on the right, which can be an expression (expression lambda) or a statement segment enclosed in curly braces (statement lambda). It omits the delegate keyword, making the code more compact. For example:
N = > n% 2percent 0
Equivalent to
Delegate (int n) {return n% 2 percent zero;}
Expression lambda is widely used in LINQ, and it can be used to construct Expression Tree,Expression Tree. It is the foundation of LINQ. Complex dynamic LINQ queries can be implemented by dynamically constructing Expression Tree, but although this method is common, it is still troublesome to use compared with traditional concatenated strings for database queries.
At this point, the study on "how to understand C # delegation, time and Lambda expressions" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.