Understanding lambda expressions step by step

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.