Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
I have an interface:
interface IFoo<out T> { T Get(); }
and some instances like IFoo<int> a, IFoo<User> u, IFoo<str
IFoo<int> a
IFoo<User> u
IFoo<str
问问题
问问题 2011-10-10T05:33:27.670 122 次 This question shows research effort; it is useful and clear 1 This question does not show any research effort; it is unclear or not useful Bookmark this question. Show activity on this post. I have an interface: interface IFoo<out T> { T Get(); } and some instances like IFoo<int> a, IFoo<User> u, IFoo<string> s and etc. There is a List<IFoo<object>> used to collect them. But variance doesn't work for value types, is there a proper way to put them in the list? It doesn't look like you need generics for this list, so you can have the interface implement a non-generic interface: interface IFoo<out T> : IFoo { } That way, all of your objects implement the same interface. This may not be a bad idea, since they do have something in common. Now you can simply use a List<IFoo>. c#.netgenericscovariancevariance 4 1 回答 1 This answer is useful 3 看起来您不需要此列表的泛型,因此您可以让接口实现非泛型接口: interface IFoo<out T> : IFoo { } 这样,您的所有对象都实现了相同的接口。这可能不是一个坏主意,因为它们确实有一些共同点。现在您可以简单地使用List<IFoo>. 于 2011-10-10T05:38:46.553 回答 Related 1 java - 安排消息驱动 bean 在特定时间访问队列? 0 android - android:在画布中缩放/平移 3 python - 鸭子类型和类方法(或者,如何使用类和实例中的方法?) 2 asp.net - 回发后未选择单选按钮 2 iphone - 标签文本中间截断 1 java - 如何从属性文件中获取 bean 验证错误消息? 3 android - 向选项卡添加视图与向选项卡 android 添加活动 2 installation - inetc::post 在 nsis 中出现“URL 部分错误” 3 perl - 如何调用具有预先分配给某个值的变量的子例程? 1 internet-explorer-8 - Internet Explorer 自动更改我的 DOCTYPE Reference php × 1429865 c/c++ × 756500 nginx × 49975 mongodb × 159057 mybatis × 3233 anaconda × 13410 pycharm × 14671 python × 1902243 vscode × 56040 docker × 110988 github × 49000 flask × 49129 ffmpeg × 24037 jmeter × 16910 matplotlib × 63493 bootstrap × 54641
and some instances like IFoo<int> a, IFoo<User> u, IFoo<string> s and etc. There is a List<IFoo<object>> used to collect them. But variance doesn't work for value types, is there a proper way to put them in the list?
IFoo<string> s
List<IFoo<object>>
It doesn't look like you need generics for this list, so you can have the interface implement a non-generic interface:
interface IFoo<out T> : IFoo { }
That way, all of your objects implement the same interface. This may not be a bad idea, since they do have something in common. Now you can simply use a List<IFoo>.
List<IFoo>
看起来您不需要此列表的泛型,因此您可以让接口实现非泛型接口:
这样,您的所有对象都实现了相同的接口。这可能不是一个坏主意,因为它们确实有一些共同点。现在您可以简单地使用List<IFoo>.