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 );
}