T[]代表数组,为什么不是IEnumerable?
为什么 C# 中 T[]
代表数组而不是 IEnumerable
?
我知道泛型是在 .NET 2.0 中引入的。如果它是从头开始设计的,那么将 []
映射到数组是否有意义?积极使用 LINQ 扩展方法,我更喜欢编写简单的 T[]
而不是庞大的 IEnumerable
或显式 .ToArray()
。
我只是出于学术原因想知道。
Why does T[]
stand for array and not IEnumerable<T>
in C#?
I know that generics were introduced in .NET 2.0. If it was designed from scratch, does it make any sense to map []
to array? Actively using LINQ extension methods, I'd prefer to write simple T[]
instead of bulky IEnumerable<T>
or explicit .ToArray()
.
I'm wondering just for academic reasons.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
T[]
是静态类型类 C 语言的数组标准语法。如果 .NET 1.0 有泛型,数组可能会使用Array
类型。无论如何,如果更有意义,因为方括号意味着索引。
T[]
用于IEnumerable
它只能在声明中使用,并且您将无法使用创建一个声明新的 T[] { ... }
。就接口而言,将其映射到 IListT[]
is the standard syntax for arrays for statically-typed C-like languages. If .NET 1.0 had generics, arrays might have used anArray<T>
type.At any rate, if
T[]
was used forIEnumerable<T>
it could only be used in declarations and you wouldn't be able to create one usingnew T[] { ... }
. As far as interfaces go, it would make more sense to map it toIList<T>
since the square brackets imply indexing.