c# - 在 C# 中使用数组实例化 IEnumerable<IEnumerable<T>>

标签 c# arrays c#-4.0

我可能错过了 C# 中数组构造语法的一些内容。如果我有以下函数(感谢 c# 4 中的类型方差功能):

IEnumerable<IEnumerable<T>> test<T>()
{
        return new List<List<T>>();
}

如何编写类似的实例化数组的数组?

最佳答案

IEnumerable<IEnumerable<T>> test<T>()
{
        return new [] { new T[] {} };
}

<子> 我注意到 Mono 编译器曾经有一个错误(至少 2.6.7 gmcs 会崩溃......),需要你拼写 new IEnumerable<T>[] { new T[] {}} ;这不再是问题,例如单声道2.11

或者

您可以使用yield block (效率较低)

IEnumerable<IEnumerable<T>> test<T>()
{
        yield return new T[] {};
}

Enumerable.Empty<>

你知道Enumerable.Empty<>吗? ?

IEnumerable<IEnumerable<T>> test<T>()
{
        return Enumerable.Empty<IEnumerable<T>>();
}

关于c# - 在 C# 中使用数组实例化 IEnumerable<IEnumerable<T>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6634615/

相关文章:

c# - 什么时候使用 Duck Typing?

javascript - 在javascript中将多个打乱的数组连接成一个大的随机数组

c - C 中的简单中值函数?

java - 将 7 个随机生成的数字和名称存储在数组 Java 中

c#-4.0 - 为什么我无法使用此代码列出我的所有帐户?

c# - 有没有办法创建支持接口(interface)的 DynamicObject?

c# - SignalR 集线器客户端在触发事件时为空

c# - 尚未为数据源 'DataSet1' 提供数据源实例

时间:2019-01-17 标签:c#evalfunction

c# - 类定义上的 "Inconsistent accessibility"