T[]代表数组,为什么不是IEnumerable

发布于 2024-12-05 09:41:30 字数 279 浏览 2 评论 0原文

为什么 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 技术交流群。

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

发布评论

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

评论(1

泛滥成性 2024-12-12 09:41:30

T[] 是静态类型类 C 语言的数组标准语法。如果 .NET 1.0 有泛型,数组可能会使用 Array 类型。

无论如何,如果 T[] 用于 IEnumerable 它只能在声明中使用,并且您将无法使用 创建一个声明新的 T[] { ... }。就接口而言,将其映射到 IList更有意义,因为方括号意味着索引。

T[] is the standard syntax for arrays for statically-typed C-like languages. If .NET 1.0 had generics, arrays might have used an Array<T> type.

At any rate, if T[] was used for IEnumerable<T> it could only be used in declarations and you wouldn't be able to create one using new T[] { ... }. As far as interfaces go, it would make more sense to map it to IList<T> since the square brackets imply indexing.

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