gpt4 book ai didi

c# - 泛型和接口(interface)枚举

转载 作者:太空宇宙 更新时间:2023-11-03 18:01:38 26 4
gpt4 key购买 nike

如果有一组类都实现了一个接口(interface)。

interface IMyinterface<T>
{
int foo(T Bar);
}

我想把它们全部塞进一个列表中并列举它们。
 List<IMyinterface> list
foreach(IMyinterface in list)
// etc...

但编译器想知道 T 是什么。我可以这样做吗?我该如何克服这个问题?

最佳答案

没有 IMyinterface 类型,只有 IMyinterface`1 类型需要类型参数。您可以创建一个 IMyinterface 类型:-

interface IMyinterface { ... }

然后继承它
interface IMyinterface<T> : IMyinterface { ... }

您需要将要在 foreach 循环中使用的任何成员移动到 IMyinterface 定义。

关于c# - 泛型和接口(interface)枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/311274/

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