<分区>
我的函数 f()
的参数必须实现两个通过继承彼此不相关的不同接口(interface),IFoo
和 IBar
.我知道有两种不同的方法可以做到这一点。第一种是声明一个继承自两者的空接口(interface):
public interface IFooBar : IFoo, IBar
{
// nothing to see here
}
public int f(IFooBar arg)
{
// etc.
}
当然,这要求这些类将自己声明为实现 IFooBar
,而不是分别实现 IFoo
和 IBar
。
第二种方法是使 f()
具有约束条件:
public int f<T>(T arg) where T : IFoo, IBar
{
// etc.
}
你更喜欢哪一个,为什么?两者有什么不明显的优点或缺点吗?