C#:您会将 IEnumerable 类命名为什么?

发布于 2024-07-27 16:47:36 字数 677 浏览 1 评论 0原文

当阅读这个问题时,我开始有些疑惑。 假设您有这两个:

class ProductCollection : ICollection<Product>
class ProductList : IList<Product>

您将 IEnumerable 中的一个称为什么?

class Product--- : IEnumerable<Product>

在我阅读其他问题之前,我实际上可能将其称为 ProductCollection ,但考虑到新信息,这会有点误导,因为它没有实现 ICollection< /代码>。 你能称之为产品吗?

var products = new Products(); // products is/are products

几乎可以工作,但听起来有点奇怪......你会怎么称呼它?

When reading this question I started to wonder a bit. Say you have these two:

class ProductCollection : ICollection<Product>
class ProductList : IList<Product>

What would you call one that were an IEnumerable<Product>?

class Product--- : IEnumerable<Product>

Before I read that other question I might have called it a ProductCollection actually, but taking the new info into account, that would have been a bit misleading since it does not implement ICollection<Product>. Could you call it Products?

var products = new Products(); // products is/are products

Almost works but sounds a bit strange... What would you call it?

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

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

发布评论

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

评论(9

一世旳自豪 2024-08-03 16:47:37

如果您查看 .NET Framework 中实现 IEnumerable的类型,我会说最常见的后缀是 Collection,后面跟着复数形式的该列表包含。 还有一些“特殊情况”(例如QueueStack)。 我个人会投票支持 Collection 作为第一手选择。

If you look at the types in the .NET Framework that implements IEnumerable<T>, I would say that the most common suffix is Collection, followed by the plural form of what the list contains. Then there are a number of "special cases" (such as Queue and Stack). I would personally vote for Collection as a first-hand choice.

怪我太投入 2024-08-03 16:47:37

在我根据自己的经验所能想到的几乎所有情况下,我都不必想出名字。 编译器为我做这件事,因为我编写迭代器方法而不是手动编写类。 对于方法名称,将其作为描述序列的复数词似乎很自然。

In almost every case I can think of from my own experience, I haven't had to think of a name. The compiler does it for me, because I write an iterator method instead of writing a class by hand. And for the method name, it seems natural to just make it a plural word describing the sequence.

梅倚清风 2024-08-03 16:47:37

就像 kek444 我想我必须问它还有什么作用? 它实现了 IEnumerable<> 但这还不是全部,否则你就不需要这门课了,对吗? 它是一个集合? 它是否将一个可枚举项转换为另一个可枚举项(改变顺序、选择、生成等)?

Like kek444 I think I'd have to ask what else does it do? It implements IEnumerable<> but that's not all it does, or else you wouldn't need the class right? It is a collection? Does it transform one enumerable to another (alter order, selection, generation, etc)?

余生再见 2024-08-03 16:47:37

我会说集合。 它可能建议使用 ReadOnlyCollection 或 ObservableCollection ,但它很好地描述了该类。 毕竟它是产品的集合(无论底层类型是什么)。

要解决这个问题和另一个问题,请使用这个;)
http://www.classnamer.com/

I would say Collection. It might suggest a ReadOnlyCollection or ObservableCollection , but it describes the class well. It is after all a collection of products (whatever the underlying type may be).

To solve this question and the other question, use this ;)
http://www.classnamer.com/

莫多说 2024-08-03 16:47:36

通常,类的名称不会基于它实现的任何接口。 (当有多个类时,您首先选择哪一个?)通常将其基于继承类,但更常见的是简单地基于类的目的,当然不是界面。 (接口可能以类命名,如果有的话。)

您的示例在某种程度上是无效的,因为精心设计的 ProductCollection 应该实现 ICollection IEnumerable 而精心设计的 ProductList 应该实现这些接口以及 IList

如果您查看 .NET Framework 的 BCL,您应该注意到情况正是如此。 List 类实现了所有三个接口,Collection 类也是如此(但请注意,在一般情况下,“集合”不需要实现>IList)。

You generally do not base the name of a class off any interface it implements. (Which one do you choose when there are multiple ones, for a start?) It is quite typical to base it off an inherited class, but more often simply on the purpose of the class, and certainly not the interface. (The interface might be named after the class, if anything.)

Your example is somewhat invalidated by the fact that a well-designed ProductCollection should implement ICollection<Product> and IEnumerable<Product> while a well-designed ProductList should implement both those interfaces as well as IList<Product>.

If you look in the BCL of the .NET Framework, you should notice that this is precisely the case. The List<T> class implements all three interfaces, as does the Collection<T> class (though note that in the general case a 'collection' need not implement IList<T>).

扎心 2024-08-03 16:47:36

我认为“序列”是一个很好的后缀。

I think that "Sequence" would be a good suffix.

热血少△年 2024-08-03 16:47:36

如果它仅实现 IEnumerable,那么我会将其命名为 ProductEnumeration,但我可以随意将其实例命名为 products。 另一方面,我不记得曾经创建过仅实现 IEnumerable 的类。 如果您无法向其中添加内容,并且如果可以的话,那么我将从实现 IEnumerable的集合类之一派生并继承该行为,这似乎没有多大意义, 也。

如果我要返回 Product 实体的枚举,我只需将其作为 IEnumerable 返回,而无需特殊的类。

If it only implements IEnumerable<Product>, then I would name it ProductEnumeration, but I would feel free to name an instance of it products. On the other hand, I don't recall ever creating a class that only implemented IEnumerable<T>. Doesn't seem to be much point if you can't add stuff to it and if you can, then I'd derive from one of the collection classes that implements IEnumerable<T> and inherit that behavior, too.

If I were returning an enumeration of Product entities, I'd simply return it as IEnumerable<Product> without having a special class.

你的呼吸 2024-08-03 16:47:36

它取决于上下文,例如,它可能是一个ProductsCatalog(暗示IEnumerable 的只读性质)。

更一般地说,它可以是 ProductsView。 当然,获取的Products是可以修改的,但我觉得它“听起来”合适。

It would depend upon the context, for example, it could be a ProductsCatalog (implying the read-only nature of IEnumerable).

More generally, it could be ProductsView. Of course, the Products fetched would be modifiable, but I feel it "sounds" appropriate nevertheless.

困倦 2024-08-03 16:47:36

这里的问题是列表或集合强加了它们的行为方式。 但 IEnumerable 非常通用,不仅仅是 C# 意义上的通用。 每个实现都可以在其背后。

一些建议:

  • ProductIterator:它迭代产品。
  • ProductRetriever:检索产品。
  • ProductCreator:这会在运行时创建新产品。

The problem here is that List or Collection impose they behave that way. But IEnumerable is very generic, not only in the C# meaning of generic. Every implementation can be behind it.

Some suggestions:

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