Generics in C# differ in many ways from C++ templates (1). You'd be right that it would call the more restrictive IEnumerable<T> function if we were working in C++ meta-programming. However, in C# the <T> function matches List<T> better, because it is more exact.

I tested your code above and it compiles just fine for me on .NET v4.0.30319 but it returns a List<T> type instead of the expected reduced IEnumerable<T> type that a Select call returns. Indicating that the <T> function was called.

If you want to perform DoSomething on all objects in IEnumerable extended classes, then here is a way to do so:

public static T DoSomething<T>(T val)
    switch (val)
        case IEnumerable vals:
            foreach (object x in vals)

    return val;

I've set it up in a way that allows for matching other specific types, as I'd guess that each different type is going to do something different. If that is not intended you can always use just a simple statement matching.

Related Articles