I have a method which I'd like to take all list-like objects in my solution. Before .NET 4.5, this was simple:
public static T Method<T>(IList<T> list)
{
// elided
}
However, .NET 4.5 introduced IReadOnlyList<T>
, which this method should also apply to.
I can't just change the signature to take an IReadOnlyList<T>
, as there are places where I apply the method to something specifically typed as an IList<T>
.
The algorithm can't run on IEnumerable<T>
, and it's used too frequently (and with too large objects) to take an IEnumerable<T>
and create a new List<T>
on every call.
I've tried adding an overload:
public static T Method<T>(IReadOnlyList<T> list)
{
// elided
}
... but this won't compile for anything which implements both interfaces (T[]
, List<T>
, and numerous other types), as the compiler can't determine which method to use (particularly annoying as they have the same body, so it doesn't matter).
I don't want to have to add overloads of Method
which take T[]
, and List<T>
, and every other type which implements both interfaces.
How should I accomplish this?