How can I write a generic anonymous method?

Solution 1:

Nope, sorry. That would require generic fields or generic properties, which are not features that C# supports. The best you can do is make a generic method that introduces T:

public Func<IList<T>, T> SelectionMethod<T>() { return list => list.First(); }

And now you can say:

Func<IList<int>, int> selectInts = SelectionMethod<int>();

Solution 2:

Of course you can, but T must be known:

class Foo<T>
{
    public Func<IList<T>, T> SelectionMethod = list => list.First();
}

As an alternative you could use a generic method if you don't want to make the containing class generic:

public Func<IList<T>, T> SelectionMethod<T>()
{
    return list => list.First();
}

But still someone at compile time will need to know this T.