Chaining methods C# with Example



Chaining methods C# with Example

Many LINQ functions both operate on an IEnumerable and also return an IEnumerable. The 
type parameters TSource and TResult may or may not refer to the same type, depending on the method in 
question and any functions passed to it. 
A few examples of this are 
public static IEnumerable Select( 
this IEnumerable source, 
Func selector 
) 
public static IEnumerable Where( 
this IEnumerable source, 
Func predicate 
) 
public static IOrderedEnumerable OrderBy( 
this IEnumerable source, 
Func keySelector 
) 
While some method chaining may require an entire set to be worked prior to moving on, LINQ takes advantage of 
deferred execution by using yield return MSDN which creates an Enumerable and an Enumerator behind the 
scenes. The process of chaining in LINQ is essentially building an enumerable (iterator) for the original set -- which 
is deferred -- until materialized by enumerating the enumerable. 
This allows these functions to be fluently chained wiki, where one function can act directly on the result of another. 
This style of code can be used to perform many sequence based operations in a single statement. 
For example, it's possible to combine SELECT, Where and OrderBy to transform, filter and sort a sequence in a single 
statement. 
var someNumbers = { 4, 3, 2, 1 }; 
var processed = someNumbers 
.Select(n => n * 2) //  Multiply  each  number  by  2 
.Where(n => n != 6) //  Keep  all the  results, except  for 6 
.OrderBy(n => n); //  Sort  in  ascending  order 
Output: 
2 
4 
8 
 

Live Demo on .NET Fiddle 
Any functions that both extend and return the generic IEnumerable type can be used as chained clauses in a 
single statement. This style of fluent programming is powerful, and should be considered when creating your own 
extension methods. 

0 Comment's

Comment Form

Submit Comment