This is similar to C# - Multiple generic types in one list
However, I want a generic method to accept a List of objects that all implement the same interface.
This code gives the error that there is no implicit reference conversion.
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?
Define T
as ITest
and take a 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>());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With