你怎么称呼 IEnumerable

发布于 2024-12-02 11:58:08 字数 400 浏览 2 评论 0原文

有时我发现自己在思考在引用 IEnumerable 时应该使用什么词。我认为在阅读时我没有看到一致的命名。

IEnumerable:这是一个类型名称。当思考或在句子中使用时它没有帮助。

集合:它有效,但并未在任何地方使用。尽管我们有 System.Collections 命名空间。

序列:它也是有效的,并且可能更好地定义它。

EnumeratorIterator:它们强调运算,很少使用。

什么是最合适的?你在代码中读到了什么?

I sometimes find myself thinking what word to use when referring an IEnumerable<Foo>. I don't think I see a consistent naming when reading.

IEnumerable<Foo>: It's a type name. It isn't helpful when thinking or using in a sentence.

Collection: It's valid but not used everywhere. Although we have the System.Collections namespace.

Sequence: It's valid too and probably defines it better.

Enumerator or Iterator: They emphasize the operation, rarely used.

What would be the most appropriate one? What do you read it in your code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

热鲨 2024-12-09 11:58:08

IEnumerable 不是迭代器/枚举器 - 对于迭代它的设备来说这是一个单独的概念。

集合通常指的是允许添加/删除等操作的特定设备;这并不适用于此,但这个术语并非没有道理。

就我个人而言,我倾向于在讨论中使用“序列”,但我不使用它来命名代码中的事物 - 我只是使用序列代表的事物,即“orders”、“uniqueUsers” ”等 - 或者在通用方法的情况下,“项目”、“值”等。

An IEnumerable<Foo> is not an iterator/enumerator - that is a separate concept for the device that iterates it.

A collection generally refers to a specific device allowing operations such as add/remove; that does not apply here, but the term is not unreasonable.

Personally I tend to use "sequence" in discussion, but I don't use that for naming things in the code - I'd just use the thing the sequence represents, i.e. "orders", "uniqueUsers", etc - or in the case of generic methods, "items", "values", etc.

旧人 2024-12-09 11:58:08

我绝对倾向于序列。在我看来,集合是有限的,IEnumerable 可以很好地表示动态生成的一系列值,因此它可以表示一个无限序列值。

I definitely lean towards sequence. In my mind, a collection is finite, and an IEnumerable<Foo> may very well represent a sequence of values that are produced on the fly, and so it may represent an infinite sequence of values.

天气好吗我好吗 2024-12-09 11:58:08

有一个以书面形式将其称为“Foo 的 IEnumerable”的先例。例如,此 MSDN 页面 引用“XElement 的 IEnumerable”。

口头上我会说“Foo的IEnumerable”或“Foo的Enumerable”。

There's a precedent for referring to it in writing as "an IEnumerable of Foo". For example, this MSDN page refers to "an IEnumerable of XElement".

Verbally I would say "An IEnumerable of Foo" or "an Enumerable of Foo".

默嘫て 2024-12-09 11:58:08

到目前为止,描述“IEnumerable”类型的变量、参数或字段的最清晰、最明确的方式是将其称为“IEnumerable”、“IEnumerable(Of foo)”或“IEnumerable of foo”。作为一名 VB 程序员,我更喜欢中间语法,特别是因为它不使用可能被误认为 HTML 标记的标点符号;在口语中,后一种形式将是最自然的发音(首字母“I”代表其自己的音节),除非涉及嵌套泛型,此时可能需要其他发音提示来指示精确的嵌套。

一些实现泛型或非泛型 (*) IEnumerable 接口的类是集合,但有些不是。有些是迭代器;有些则不然。因此,除非知道特定的 IEnumerable 实际上是集合或迭代器,否则术语“集合”和“迭代器”并不真正合适。术语“迭代器”还有一个与术语“序列”相同的额外问题:并非所有迭代器或序列都实现 IEnumerable;有些实现 IEnumerator,有些实现 GetEnumerator 方法但不实现 IEnumerable(例如,因为他们希望允许“foreach”,但不希望承诺 IEnumerable 隐含的所有行为)。如果希望避免术语,“可重用序列”一词可能是一个不错的选择。它不是很明确,因为它可能引用具有 GetEnumerator 方法但未实现 IEnumerable 的类,或者可能引用其 Reset 方法实际上重置枚举的 IEnumerator 对象,但它不包括一次性使用的迭代器。

(*)从这里开始,术语“IEnumerable”将指IEnumerable和IEnumerable。可以互换。同样“IEnumerator”。

By far, the clearest and most unambiguous way to describe a variable, parameter, or field of type "IEnumerable<Foo>" is to call it an "IEnumerable<foo>", "IEnumerable(Of foo)", or "IEnumerable of foo". Being a VB programmer, I prefer the middle syntax, especially since it doesn't use punctuation that can be mistaken for HTML markup; in speaking, the latter form would be the most natural pronunciation (with the initial "I" representing its own syllable) except in cases involving nested generics, when other pronunciation cues may be required to indicate the precise nesting.

Some of the classes which implement generic or non-generic (*) IEnumerable interfaces are collections, but some are not. Some are iterators; some not. The terms "collection" and "iterator" are thus not really suitable unless one knows that a particular IEnumerable will in fact be a collection or iterator. The term "iterator" has an additional problem, shared by the term "sequence": not all iterators or sequences implement IEnumerable; some implement IEnumerator, and some implement a GetEnumerator method but do not implement IEnumerable (e.g. because they wish to allow "foreach" but do not wish to promise all the behaviors implied by IEnumerable). If one wishes to avoid jargon, the term "reusable sequence" may be a good one. It's not quite unambiguous, because it could possibly refer to classes which have a GetEnumerator method but don't implement IEnumerable, or possibly to IEnumerator objects whose Reset method actually resets the enumeration, but it excludes one-time-use iterators.

(*) The term "IEnumerable" from here on out will refer to IEnumerable and IEnumerable<T> interchangeably. Likewise "IEnumerator".

公布 2024-12-09 11:58:08

在句子(代码)中使用它。

IEnumerable<Foo> myFoos = GetFoos();
  //internal vocalization: get some Foos, assign them to myFoos

我怀疑大多数程序员不会在内部说出变量声明/赋值中的类型。

public IEnumerable<Foo> ProcessFoos(IEnumerable<Foo> source)
  //internal vocalization: The method requires some foos and returns some foos.

我想我用“一些”作为我的内部发声。也许任何复数词都可以。批量、块、束、群、过多等。

Use it in a sentence (code).

IEnumerable<Foo> myFoos = GetFoos();
  //internal vocalization: get some Foos, assign them to myFoos

I suspect that most programmers do not internally vocalize the type in a variable declaration/assignment.

public IEnumerable<Foo> ProcessFoos(IEnumerable<Foo> source)
  //internal vocalization: The method requires some foos and returns some foos.

I guess I use "some" as my internal vocalization. Probably any plural'ish word would work. Batch, chunk, bunch, flock, plethora, etc.

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