ExtensionMethod.NET Home of 820 C#, Visual Basic, F# and Javascript extension methods

Chain

Allows chaining together actions to be taken place on the fly. It works with any object. Its a simple concept but I couldn't find any examples that does the same.

Source

public static T Chain<T>(this T source, Action<T> action)
{
    action(source);
    return source;
}

Example

var l = new Collection<SelectListItem>{
 new SelectListItem{ Text="t0", Value="y0" },
 new SelectListItem{ Text="t1", Value="y1" },
 new SelectListItem{ Text="t2", Value="y2" },
 new SelectListItem{ Text="t2", Value="y3" },
}.Chain(o => o.ForEachYield(m => m.Selected = (m.Value == "y3")));

Author: Daniel Gidman

Submitted on: 30 nov 2010

Language: C#

Type: System.Variant

Views: 3528