In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what the related concepts of .NET Framework Lambda expressions are. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.
.net Framework applications provide us with a variety of development environments that meet the needs of developers in actual programming. The editor introduces the. NET Framework Lambda expression. This expression can be used as a method pointer instead of delegate.
Delegate is required to define method pointers in Category 2.0 and Cobb 1.x. As shown in the following code:
Public delegate bool Filter (int num)
/ / delegate type
Public int [] searchArray (int []
Values, Filter filter)
{
List result = new List ()
Foreach (int i in values)
{
If (filter (I))
Result.Add (I)
}
Return result.ToArray ()
}
As you can see from the code of the. NET Framework Lambda expression above, a delegate type called Filter is defined. The second parameter of the searchArray method then passes the method pointer to the method through the Filter type. In fact, as you can see from IL, Filter is compiled into a class that is a subclass of System.MulticastDelegate. But unlike ordinary classes, System.MulticastDelegate and its subclasses can only be used by the compiler and cannot be written directly in the source program. That is, it can only be used in IL.
If you want to call the searchArray method, you need to define a method that has the same parameters and return value as Filter. The code is as follows:
Public bool MyFilter (int num) {return ((num & 1) = = 0);}
The following code calls the searchArray method:
Int [] intArray = new int []
{1, 2, 3, 4, 5, 6, 7, 8}
Int [] evenArray = searchArray
(intArray, MyFilter)
Foreach (int i in evenArray)
TextBox1.AppendText (i.ToString ())
Developers can replace delegate with inline code through anonymous methods. For example, the above calling code can be modified to the following form:
Int [] intArray = new int []
{1, 2, 3, 4, 5, 6, 7, 8}
Int [] evenArray = searchArray
(intArray, delegate (int I) {
Return ((I & 1) = = 0);})
Foreach (int i in evenArray)
TextBox1.AppendText (i.ToString ())
The above code converts anonymous methods to delegate.
However, Lambda expressions are supported starting with Cobb 3.0 (. Net framework3.5). The so-called Lambda expression is the abbreviated form of delegate and anonymous methods. The syntax of the .NET Framework Lambda expression is as follows:
(param1, param2..., paramN) = > {expression 1; expression 2; return return value;}
The param1...paramN in the above syntax represents the parameters of the method (no need to determine the type, the C # compiler will do this for us), and the content in {.} is exactly the same as in the method body.
If delegate has no parameters, you can just write (), as shown in the following method:
Public delegate void Method1 ()
Public void test ()
{
Method1 method1 = () = >
{int I = 4; I + = 6;}
}
If delegate has only one parameter, the parentheses on both sides of the parameter can be left unwritten. The code is as follows:
Public delegate void
Method2 (int I)
Public void test ()
{
Method2 method2 = I = >
{ifinished products; I + = 6;}
}
If delegate has a return value, the * statement in {...} needs to use return to return the corresponding value. The code is as follows:
Public delegate int Method3
(int x, int y)
Public void test ()
{
Method3 method3 = (x, y) = >
{x-ray; y-ray; return x + y;}
}
If you rewrite the example at the beginning of this article using .NET Framework Lambda expressions, it will be very simple, with the following code:
Int [] intArray = new int []
{1, 2, 3, 4, 5, 6, 7, 8}
Int [] evenArray = searchArray
(intArray, I = > {return (I & 1)
= = 0;})
Foreach (int i in evenArray)
TextBox1.AppendText (i.ToString ())
Of course, we can also assign the Lamdba expression to a delegate and pass the value as follows
Filter filter = I = > {return
(I & 1) = = 0;}
Int [] intArray = new int []
{1, 2, 3, 4, 5, 6, 7, 8}
Int [] evenArray = searchArray
(intArray, filter)
Foreach (int i in evenArray)
TextBox1.AppendText (i.ToString ())
In essence, after A. NET Framework Lambda expression is compiled by the C # compiler, it still changes to the form of delegate, which means that the Lamdba expression is only an improvement at the syntax level, not a new instruction provided by IL. Such as the following two lines of code are equivalent:
Filter filter = I = >
{return (I & 1) = = 0;}
Filter filter = delegate (int I)
{return ((I & 1) = = 0);}
Net Framework Lambda expression related concepts are shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.