System. LINQ. the enumerable class provides many extension methods. Generally, the type of the ienumerable <t> interface can be extended. For example, the orderby method is defined in the enumerable class, this method is used to sort the sequence of target values based on a key value. The Int [] type can have the orderby method.
The orderby method has two parameters:ThisIenumerable <tsource> source; one is the delegate that returns the sort key value: func <tsource, tkey> keyselector. Among them, func is a tsource type parameter, which returns the delegate of the tkey type value. It must be said that this parameter can be written in multiple ways: 1,
1. directly use lambda expressions:
1 Int [] Sets = { 1 , 3 , 6 , 4 , 3 , 8 , 7 };
2 VaR subset = Sets. orderby (( Int A) => { Return A ;});
3 Foreach (VAR temp In Subset)
4 {
5 Console. Write (temp );
6 }
2. Use the anonymous method:
1 Int [] Sets = { 1 , 3 , 6 , 4 , 3 , 8 , 7 };
2 VaR subset = Sets. orderby ( Delegate ( Int A ){ Return A ;});
3 Foreach (VAR temp In Subset)
4 {
5 Console. Write (temp );
6 }
3. New delegate:
1 Int Key ( Int A)
2 {
3 Return A;
4 }
5
6 Int [] Sets = { 1 , 3 , 6 , 4 , 3 , 8 , 7 };
7 VaR subset = Sets. orderby ( New Func < Int , Int > (Key ));
8 Foreach (VAR temp In Subset)
9 {
10 Console. Write (temp );
11 }
Any new delegate (New mydelegate (…) required (......)) Generally, you can use the anonymous method (delegate (parameter ){......}), You can also use lambda expressions (parameters) =>{ function body }).