为什么我必须转换为类型参数而不能使用约束类型?

mrt*_*181 1 c# generics casting generic-variance

任何人都可以解释为什么我必须转换为T,为什么Add2不接受Bar作为参数?

class Foo<T> where T : class, IBar
{
    void Add1(IDo<T> @do) { @do.Stuff(new Bar() as T); }

    // Add2 does not compile:
    // Argument type Bar is not assignable to parameter Type T
    void Add2(IDo<T> @do) { @do.Stuff(new Bar()); } 
}

interface IBar {}

class Bar : IBar {}

interface IDo<in T> {
    void Stuff(T bar);
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 6

这可能不合适.例如,考虑:

class Other : Bar {}

...

IDo<Other> do = new DoImpl<Other>();
Foo<Other> foo = new Foo<Other>();
foo.Add2(do);
Run Code Online (Sandbox Code Playgroud)

使用您当前的代码,这将是调用do.Add2(new Bar())...这显然是无效的,因为a Bar不是Other,需要IDo<Other>.Stuff.

转换为T(或使用as)也不合适 - 你不能转换new Bar()Other,如果你使用as你将只获得一个空引用.