What\'s the standard way to get a typed, readonly empty list in C#, or is there one?
ETA: For those asking \"why?\": I have a virtual method that re
To expand on Dan Tao's answer, the following implementation can be used in the same way as Enumerable.Empty, by specifying List.Empty instead.
public static class List
{
public static IList Empty()
{
// Note that the static type is only instantiated when
// it is needed, and only then is the T[0] object created, once.
return EmptyArray.Instance;
}
private sealed class EmptyArray
{
public static readonly T[] Instance = new T[0];
}
}
Edit: I change the above code to reflect the outcome of a discussion with Dan Tao about lazy versus eager initialization of the Instance field.