If I wanted to create a method that takes an instance of IList as a parameter (or any other interface, but let's use IList as an example), I could create a generic method with a type constraint, e.g.:
public static void Foo1<T>(T list) where T : IList
{
}
Alternatively, I could create a method that takes an IList parameter directly:
public static void Foo2(IList list)
{
}
For all intents and purposes, it seems like these methods behave exactly the same:
List<string> myList = new List<string>();
Foo1(myList);
Foo2(myList);
So here's my question -- what's the difference between these two approaches? It seems like the second approach is slightly more readable; are there any other differences I should be aware of (different IL being generated, etc)? Thanks in advance.
A couple of differences:
Tcould be a value type and still end up being unboxed in the generic form. It's pretty unlikely that this would be the case forIList, but for other interfaces it's highly plausibleTisIt really depends on what you're doing of course... unless you actually need to know anything about
Twithin the method, the only benefit to the generic form is the boxing point.