我可以在 C# 中拥有非类型化集合吗
我正在将一些 Java 代码移植到 C#,并且遇到了这个问题:
List<?>
据我了解,这是一个 Unknown
类型的 List
。因此,我可以在其他地方指定类型(在运行时?我不确定)。
C# 中的基本等价物是什么?
I am porting some Java code to C# and I ran across this:
List<?>
As I understand it this is a List
of type Unknown
. As a result I can dictate the type elsewhere (at runtime? I'm not sure).
What is the fundamental equivalent in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为与 Java 的
List
最好的匹配是 C# 4.0IEnumerable
如果您有一个方法需要List
比您可以使用List
C# 4.0
IEnumerable
接口实际上是IEnumerable
,这意味着如果R
派生自T
,则IEnumerable
code> 可以从IEnumerable
分配。因此,您所要做的就是将
doSomething
转换为DoSomething
并接受IEnumerable
参数:编辑: 如果 C# 4.0 不可用,您始终可以回退到无类型的
IEnumerable
或IList
。I think the best match to Java's
List<?>
would be C# 4.0IEnumerable<out T>
If you have a method that takesList<?>
than you can call it withList<Object>
andList<String>
like so:C# 4.0
IEnumerable<T>
interface is actuallyIEnumerable<out T>
, which means that if, say,R
derives fromT
,IEnumerable<T>
can be assigned to fromIEnumerable<R>
.So, all you have to do is make your
doSomething
intoDoSomething
and have acceptIEnumerable<T>
parameter:EDIT: If C# 4.0 is not available, you can always fall back to either untyped
IEnumerable
orIList
.如果您想要一个可以容纳任何内容的列表,可以使用
List
如果您想要一个包含未知类型的强类型列表,您应该创建一个泛型类或方法并使用
List
。如需更具体的建议,请提供更详细的信息。
If you want a list that can hold anything, you can use a
List<object>
or anArrayList
.If you want a strongly-typed list that holds an unknown type, you should make a generic class or method and use a
List<T>
.For more specific advice, please provide more detail.
首先,正如其他地方提到的,无界通配符参数化类型与 Object 不同。泛型不是协变的。因此,在OP的Java示例中,
List
与List
Java 中
List
的唯一实际用例是与遗留的非泛型集合交互时。它可以让您避免未经检查的转换警告来自编译器。C# 没有这个用例。 C# 泛型不是使用擦除来实现的。 .net 中的泛型和非泛型集合之间不存在向后兼容性 - 它们共存于核心 .net 库中。这与 Java 不同,在 Java 中,集合 api 的通用版本取代了 JDK 1.5 中的非通用版本。
所以我认为没有理由在 C# 中使用此构造,并且没有以相同方式运行的直接等效项。
Firstly, as mentioned elsewhere, the unbounded wildcard parameterized type is not the same as Object. Generics are not covariant. So in the OP's Java example
List<?>
is not the same asList<Object>
. As an example,The only real use case for
List<?>
in Java is when interacting with legacy non generic collections. It allows you to avoid unchecked conversion warnings from the compiler.C# does not have this use case. C# generics were not implemented using erasure. There is no backwards compatibility between the generic and non-generic collections in .net - they co-exist in the core .net libraries. That is different from Java where the generic version of the collections api replaced the non generic version at JDK 1.5.
So I don't think there is a reason to want this construct in C#, and there is no direct equivalent that behaves in the same way.
听起来
IList
就是您正在寻找的东西。它是列表的通用接口,这意味着您必须强制转换出现的任何内容,并小心输入的内容。It sounds like
IList
is what you're looking for. It's a generic interface for lists, meaning you'll have to cast anything that comes out and be careful what you put in.我认为提问者想要将这样的东西转换
成 C# 等价物。
以下代码不起作用,因为它只接受
List
I think the questioner wants to convert something like this
into C# equivalent.
The following code won't work, because it only accept
List<Object>
, notList<Foo>
你可以只使用
You could just use