gpt4 book ai didi

C#:需要实现两个接口(interface)的参数的函数的首选模式

转载 作者:太空狗 更新时间:2023-10-29 17:54:21 26 4
gpt4 key购买 nike

<分区>

我的函数 f() 的参数必须实现两个通过继承彼此不相关的不同接口(interface),IFooIBar .我知道有两种不同的方法可以做到这一点。第一种是声明一个继承自两者的空接口(interface):

public interface IFooBar : IFoo, IBar
{
// nothing to see here
}

public int f(IFooBar arg)
{
// etc.
}

当然,这要求这些类将自己声明为实现 IFooBar,而不是分别实现 IFooIBar

第二种方法是使 f() 具有约束条件:

public int f<T>(T arg) where T : IFoo, IBar
{
// etc.
}

你更喜欢哪一个,为什么?两者有什么不明显的优点或缺点吗?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com