Home » ASP.NET » Book...

Book Notes - method groups, transformations, and anonymous methods

reading notes - method group conversion and anonymous methods, there is a need for friends can refer to.


method, group conversion, and anonymous method


The article introduced

, C# 1 in the Commission of the key concept in C# 2, commissioned by the improved 2 by improved.C# greatly and provide the groundwork for new features in C# 3, when we understand the anonymous method, Lambda learning will become relatively easy to.


below look at the improvements.

commissioned in C# 2
The

method group converts


in C# 1, if you want to create an instance of the delegate, you must also specify a delegate type and method to match the delegate signature. However, in C# 2, support method group conversion, that is to say we can from the group to a compatible implicit delegate type conversion. The so-called "group" (method group), is actually a method name.


, look at an example:


replication code

Class Program

{

Public, delegate, void, ReverseStringHandler (string, STR);


private, static, void, ReverseString (string, STR)
{
Char[] charArray = str.ToCharArray ();
Array.Reverse (charArray);
Console.WriteLine (charArray);
}
Static void Main (string[] args)
{
Create delegate instance in //C# 1
ReverseStringHandler reverseStringHandler = new ReverseStringHandler (ReverseString);
ReverseStringHandler ("Hello World");
In //C# 2, a delegate instance is created by method group transformation
ReverseStringHandler = ReverseString;
ReverseStringHandler ("Good morning");
}

}

Copy code

Through the method group transformation, the tedious delegate instance creation was simplified.


anonymous method


, according to what we've learned in C# 1, when we create a delegate instance, we need to find a method consistent with the delegate type signature that uses this method to instantiate a delegate object,.


look at the example of string inversion in the front, and maybe "ReverseString". This method is used only once in the program, but this method must have.

in order to create a delegate instance

introduced anonymous methods in C# 2, so there is a simpler way to implement the example above:


replication code

Public, delegate, void, ReverseStringHandler (string, STR);


static void Main (string[] args)

{

ReverseStringHandler reverseStringHandler = delegate (string, STR)

{

Char[] charArray = str.ToCharArray ();

Array.Reverse (charArray);

Console.WriteLine (charArray);

};

ReverseStringHandler ("Hello World");


Console.Read ();

}

Copy code

As you can see above, anonymous methods are implemented through the delegate keyword as well as the parameter list and the specific statement block.


so, if the method used to instantiate delegates is relatively simple and the method is used very frequently elsewhere, you can consider using anonymous methods to simplify.


The working principle of

anonymous method


if you're still wondering about anonymous methods, I suggest you look at the IL code.

for the example above

in the IL code of the Main function, you can see that the compiler has generated a method named b__0 for us to accept a parameter string of type.


replication code

.method, private, hidebysig, static,

Void Main (

String[] args

) CIL, managed,

{


.
IL_0009:, ldftn, void, AnonymousMethod.Program:: 'b__0' (string)


.
Of method Program:} / / end: Main

Copy code

When we look at the IL code of the b__0 method, you can see that this method is the operation.

we defined in the statement block of the anonymous method

replication code

.method, private, hidebysig, static,

Void 'b__0' (

String str

) CIL, managed,

{

.custom, instance, void, [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.Ctor () = (

01000000


)
Begins at RVA 0x2050
/ / Method
Code / size 23 (0x17)

.maxstack 1,

.locals init (

[0] char[] charArray

)
IL_0000: NOP
IL_0001: ldarg.0
IL_0002:, callvirt, instance, char[], [mscorlib]System.String:: ToCharArray ()
IL_0007: stloc.0
IL_0008: ldloc.0
IL_0009:, call, void, [mscorlib]System.Array:: Reverse (class, [mscorlib]System.Array)
IL_000e: NOP
IL_000f: ldloc.0
IL_0010: call void [mscorlib]System.Console:: WriteLine (char[])
IL_0015: NOP
IL_0016: ret

} of: Program: / / end method 'b__0'

Copy code

The anonymous method principle is: the anonymous method in the class, for each of the anonymous method have created a method. These methods are effective only in the compiler to create the IL code in the C# code, C# code is invalid, so these methods cannot be used directly in /p>.
In fact,

, where the anonymous method is more common to the anonymous method as a parameter to another way. You must know the List have a FindAll to find the qualified item, where FindAll is a parameter filtering condition by.


List, numList = new, List, {1, 2, 3, 4, 5,, 6, 7, 8, 9};
Foreach (int, num, in, numList.FindAll (delegate (int, n), {return, n>6,}))

{

Console.WriteLine (Num);


}
When it comes to here, we will look at the principal system to help us to define the Action in C#, Func, and Predicate is the principal system definition, we can directly use the above parameters. FindAll Predicate is a generic delegate.


Action delegate


Action is a generic delegate with no return value, and the Action delegate can support at least 0 arguments, at most 16 arguments,.


, for example:


Action stands for no arguments, no return values for the delegate

Action


Latest