If I have a general method like this:
static void Foo<T>() where T : class { }
I could call it by specifying the type of interface, for example ICollection<int>
Foo<ICollection<int>>();
Now suppose I have a structure that implements ICollection<int>
:
struct Bar : ICollection<int>
I cannot specify it explicitly as a type argument, but if I have a variable of type ICollection<int>
that has a base type of Bar
and changes its general method to accept an argument of type T
, I can do:
static void Foo<T>(T arg) where T : class { } ICollection<int> bar = new Bar(); Foo <ICollection<int>>(bar);
As you can see, it completely ignores the general constraint. So my question is why is specifying an interface as a type argument allowed if we have a class
constraint?
source share