C # considerate-if you plan to write an extention method similar to system. LINQ. enumerable. Where

Source: Internet
Author: User

C # How considerateProgramRather than. NET Framework.

 

If you want to write an extention method similar to system. LINQ. enumerable. Where, assuming the name is "filter", the following figure shows how C # can meet this requirement:

Public static ienumerable <t> filter <t> (ThisIenumerable <t> source, func <t, bool> predicate)
{
If (Source = NULL | predicate = NULL)
{
Throw new argumentnullexception ();
}
Return impl (source, predicate );
}

Private Static ienumerable <t> impl <t> (ienumerable <t> source, func <t, bool> predicate)
{
Foreach (T item in source)
{
If (predicate (item ))
{
YieldReturn item;
}
}
}

 

If you don't rely on the C # sugar-coated language, that is, the two simhei keywords above, purely relying on the framework for implementation, you will have to spend a lot of effort. Just a guess, without the thoughtfulness of C #, there won't be many brilliant LINQ providers!

 

The following is an example of using filter.Code, Filter is equivalent to where! In addition, IDE intelliisense will recognize the filter. How is it considerate?

List <product> Products = product. getsampleproducts ();

Foreach (product in products.Filter(P => P. weight> 0 ))// You can change it to where

{

Console. writeline (product );

}

 

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.