小编Tom*_*lan的帖子

明确实现的接口和泛型约束

interface IBar { void Hidden(); }

class Foo : IBar { public void Visible() { /*...*/ } void IBar.Hidden() { /*...*/ } }

class Program
{
    static T CallHidden1<T>(T foo) where T : Foo
    {
        foo.Visible();
        ((IBar)foo).Hidden();   //Cast required

        return foo;
    }

    static T CallHidden2<T>(T foo) where T : Foo, IBar
    {
        foo.Visible();
        foo.Hidden();   //OK

        return foo;
    }
}
Run Code Online (Sandbox Code Playgroud)

是否有任何区别(CallHidden1与CallHidden2)是实际编译的代码?T:Foo和T:Foo,IBar(如果Foo实现IBar)在访问显式实现的接口成员时是否存在其他差异?

c# generics explicit-interface

7
推荐指数
2
解决办法
803
查看次数

标签 统计

c# ×1

explicit-interface ×1

generics ×1