I am using generics to translate Java code to C# and having trouble with containers of the sort:
public static class MyExtensions
{
public static void add(this List<object> list, object obj)
{
list.Add(obj);
}
public static void add(this List<string> list, string s)
{
list.Add(s);
}
}
It seems that the generics are lost in comparing arguments and the two methods collide. I'd like any advice on whether generics can be used in this way. Is it possible to support all list operations with a single:
public static void add(this List<object> list, object obj)
{
list.Add(obj);
}
for example?
SUMMARY All responses have the same solution. List can be abstracted to ICollection. Overall it's probably not a good idea for production code.