我可以在 C# 中拥有非类型化集合吗

发布于 2024-09-14 03:43:25 字数 191 浏览 1 评论 0原文

我正在将一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

逆光下的微笑 2024-09-21 03:43:25

我认为与 Java 的 List 最好的匹配是 C# 4.0 IEnumerable 如果您有一个方法需要 List 比您可以使用 ListList 调用它,如下所示:

List<Object> objList = new List<Object>();
List<String> strList = new List<String>();

doSomething(objList); //OK
doSomething(strList); //OK

public void doSomething(List<?> theList) {
 ///Iterate through list
}

C# 4.0 IEnumerable 接口实际上是 IEnumerable,这意味着如果 R 派生自 T,则 IEnumerable code> 可以从 IEnumerable 分配。

因此,您所要做的就是将 doSomething 转换为 DoSomething 并接受 IEnumerable 参数:

List<Object> objList = new List<Object>();
List<String> strList = new List<String>();

DoSomething(objList); //OK
DoSomething(strList); //OK

public void DoSomething<T>(IEnumerable<T> theList) {
 ///Iterate through list
}

编辑: 如果 C# 4.0 不可用,您始终可以回退到无类型的 IEnumerableIList

I think the best match to Java's List<?> would be C# 4.0 IEnumerable<out T> If you have a method that takes List<?> than you can call it with List<Object> and List<String> like so:

List<Object> objList = new List<Object>();
List<String> strList = new List<String>();

doSomething(objList); //OK
doSomething(strList); //OK

public void doSomething(List<?> theList) {
 ///Iterate through list
}

C# 4.0 IEnumerable<T> interface is actually IEnumerable<out T>, which means that if, say, R derives from T, IEnumerable<T> can be assigned to from IEnumerable<R>.

So, all you have to do is make your doSomething into DoSomething and have accept IEnumerable<T> parameter:

List<Object> objList = new List<Object>();
List<String> strList = new List<String>();

DoSomething(objList); //OK
DoSomething(strList); //OK

public void DoSomething<T>(IEnumerable<T> theList) {
 ///Iterate through list
}

EDIT: If C# 4.0 is not available, you can always fall back to either untyped IEnumerable or IList.

人间☆小暴躁 2024-09-21 03:43:25

如果您想要一个可以容纳任何内容的列表,可以使用 ListArrayList

如果您想要一个包含未知类型的强类型列表,您应该创建一个泛型类或方法并使用 List

如需更具体的建议,请提供更详细的信息。

If you want a list that can hold anything, you can use a List<object> or an ArrayList.

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.

煮酒 2024-09-21 03:43:25

首先,正如其他地方提到的,无界通配符参数化类型与 Object 不同。泛型不是协变的。因此,在OP的Java示例中,ListList不同。例如,

// Unbounded wildcard type is not the same as Object...
List<?> unboundedList = new ArrayList<Object>(); 
List<Object> objectList = new ArrayList<Object>();
unboundedList = objectList;    // no problems
objectList = unboundedList;    // whoops! compile time error

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 as List<Object>. As an example,

// Unbounded wildcard type is not the same as Object...
List<?> unboundedList = new ArrayList<Object>(); 
List<Object> objectList = new ArrayList<Object>();
unboundedList = objectList;    // no problems
objectList = unboundedList;    // whoops! compile time error

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.

独闯女儿国 2024-09-21 03:43:25

听起来 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.

九局 2024-09-21 03:43:25

我认为提问者想要将这样的东西转换

int size2(List<?> list)
{
    return 2*list.size(); 
}

List<Foo> list = ...
size2(list);

成 C# 等价物。

以下代码不起作用,因为它只接受 List,而不接受 List

int size2(List<Object> list)
{
    return 2*list.size(); 
}

I think the questioner wants to convert something like this

int size2(List<?> list)
{
    return 2*list.size(); 
}

List<Foo> list = ...
size2(list);

into C# equivalent.

The following code won't work, because it only accept List<Object>, not List<Foo>

int size2(List<Object> list)
{
    return 2*list.size(); 
}
暗地喜欢 2024-09-21 03:43:25

你可以只使用

列表<对象>

You could just use

List<object>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文