1. simplified generic methods
Public Static Void WL < T > (T input)
{
Console. writeline (input );
}
Call:
Int A = 100 ;
WL < Int > ();
WL ();
// The C # editor calls WL to obtain the parameter type, so do not assign the generic type to the method.
2. generic delegation:
Define a generic delegate del first
Public Delegate Void Del < T > (T item );
Public Static Void WL < T > (T input)
{
Console. writeline (input );
}
Int A = 100 ;
Int B = 200 ;
Int C = 300 ;
// Del <int> m1 = new del <int> (WL <int> ); // The effect is the same as that in the downstream mode. You do not need to pass the method type (see figure 1)
Del < Int > M1 = New Del < Int > (WL );
// Anonymous Delegation
Del < Int > M2 = Delegate ( Int Input)
{
Console. writeline (input );
} ;
// Further, the new function of C #2.0 method group conversion applies to the specific delegate type and generic delegate type.
Del < Int > M3 = Wl;
M1 ();
M2 (B );
M3 (C );
3. Lambda expressions and extension methods
As follows:
Ilist < Int > List = New List < Int > ();
List. Add ( 4 );
List. Add ( 5 );
List. Add ( 6 );
VaR mylist = List. Where (I => I > 5 );
Foreach (VAR item In Mylist)
{
Console. writeline (item );
}
If you know that I => I> 5 actually represents a delegate,
Such delegation
Delegate ( Int Input)
{
ReturnInput>5;
} ;
Remember that this kind of thing is a delegate, so proceed:
Construct an extension method: (DEL is the generic delegate defined earlier, and you know the syntax of the extension method)
Public Static Void EXT < T > ( This T source, del < T > D)
{
D (source );
}
// A. Ext <int> (M1 ); // The reason is described in 1.
A. Ext (M1 ); // Is a delegate.
A. Ext (I => Console. writeline (I )); // Try this. Then try to understand other methods in the LINQ namespace.
TestCode:
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Namespace Leleapplication1
{
// Extension methods must be defined in static classes.
Public Static Class Program
{
Public Delegate Void Del < T > (T item );
Public Static Void WL < T > (T input)
{
Console. writeline (input );
}
Public Static Void EXT < T > ( This T source, del < T > D)
{
D (source );
}
Static Void Main ( String [] ARGs)
{
Int A = 100 ;
Int B = 200 ;
Int C = 300 ;
// Del <int> m1 = new del <int> (WL <int> ); // Same effect as downstream
Del < Int > M1 = New Del < Int > (WL );
Del < Int > M3 = Delegate ( Int Input)
{
Console. writeline (input );
} ;
// C #2.0 new function of method group conversion, applicable to the specific delegate type and generic delegate type
Del < Int > M2 = Wl;
M1 ();
M2 (B );
M3 (C );
// A. Ext <int> (M1 );
A. Ext (M1 );
A. Ext (I => Console. writeline (I ));
}
}
}
The above is my understanding