Another set I use quite often is for coalescing IDictionary methods:
public static TValue Get(this IDictionary d, TKey key, Func valueThunk)
{
TValue v = d.Get(key);
if (v == null)
{
v = valueThunk();
d.Add(key, v);
}
return v;
}
public static TValue Get(this IDictionary d, TKey key, TValue coalesce)
{
return Get(d, key, () => coalesce);
}
And for working with collections in general:
public static IEnumerable AsCollection(this T item)
{
yield return item;
}
Then for tree-like structures:
public static LinkedList Up(this T node, Func parent)
{
var list = new LinkedList();
node.Up(parent, n => list.AddFirst(n));
return list;
}
So I can then easily traverse and operate upon a up a class like:
class Category
{
public string Name { get; set; }
public Category Parent { get; set; }
}
Next, to facilitate function composition and a more F# like way of programming in C#:
public static Func Func(this Func f)
{
return f;
}
public static Func Compose(this Func f, Func g)
{
return x => g(f(x));
}