Several methods of writing parameters for Enumerable member functions

Source: Internet
Author: User

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: this IEnumerable <TSource> source, and 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 ;});
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 ;});
3 foreach (var temp in subset)
4 {
5 console. write (temp );
6}

 

3. New delegate:

1 int key (int)
2 {
3 return;
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 }).

Related Article

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.