General method, where T is a List implementing an interface

This is similar to C # - several common types in one list

However, I want the generic method to accept a list of objects that implement the same interface.

This code gives an error that there is no implicit link translation.

public interface ITest { } public class InterfaceUser : ITest { } public class TestClass { void genericMethod<T>(T myList) where T : List<ITest> { } void testGeneric() { genericMethod(new List<InterfaceUser>()); } } 

Can this be done?

+4
source share
1 answer

Define T as ITest and take List<T> as argument

 public interface ITest { } public class InterfaceUser : ITest { } public class TestClass { void genericMethod<T>(List<T> myList) where T : ITest { } void testGeneric() { this.genericMethod(new List<InterfaceUser>()); } } 
+7
source

All Articles