C#:您会将 IEnumerable 类命名为什么?
当阅读这个问题时,我开始有些疑惑。 假设您有这两个:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您查看 .NET Framework 中实现 IEnumerable的类型,我会说最常见的后缀是
Collection
,后面跟着复数形式的该列表包含。 还有一些“特殊情况”(例如Queue
和Stack
)。 我个人会投票支持Collection
作为第一手选择。If you look at the types in the .NET Framework that implements
IEnumerable<T>
, I would say that the most common suffix isCollection
, followed by the plural form of what the list contains. Then there are a number of "special cases" (such asQueue
andStack
). I would personally vote forCollection
as a first-hand choice.在我根据自己的经验所能想到的几乎所有情况下,我都不必想出名字。 编译器为我做这件事,因为我编写迭代器方法而不是手动编写类。 对于方法名称,将其作为描述序列的复数词似乎很自然。
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.
就像 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)?
我会说集合。 它可能建议使用 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/
通常,类的名称不会基于它实现的任何接口。 (当有多个类时,您首先选择哪一个?)通常将其基于继承类,但更常见的是简单地基于类的目的,当然不是界面。 (接口可能以类命名,如果有的话。)
您的示例在某种程度上是无效的,因为精心设计的
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 implementICollection<Product>
andIEnumerable<Product>
while a well-designedProductList
should implement both those interfaces as well asIList<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 theCollection<T>
class (though note that in the general case a 'collection' need not implementIList<T>
).我认为“序列”是一个很好的后缀。
I think that "Sequence" would be a good suffix.
如果它仅实现的集合类之一派生并继承该行为,这似乎没有多大意义, 也。
IEnumerable
,那么我会将其命名为ProductEnumeration
,但我可以随意将其实例命名为products
。 另一方面,我不记得曾经创建过仅实现IEnumerable
的类。 如果您无法向其中添加内容,并且如果可以的话,那么我将从实现 IEnumerable如果我要返回 Product 实体的枚举,我只需将其作为
IEnumerable
返回,而无需特殊的类。If it only implements
IEnumerable<Product>
, then I would name itProductEnumeration
, but I would feel free to name an instance of itproducts
. On the other hand, I don't recall ever creating a class that only implementedIEnumerable<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 implementsIEnumerable<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.它取决于上下文,例如,它可能是一个
ProductsCatalog
(暗示IEnumerable
的只读性质)。更一般地说,它可以是
ProductsView
。 当然,获取的Products
是可以修改的,但我觉得它“听起来”合适。It would depend upon the context, for example, it could be a
ProductsCatalog
(implying the read-only nature ofIEnumerable
).More generally, it could be
ProductsView
. Of course, theProducts
fetched would be modifiable, but I feel it "sounds" appropriate nevertheless.这里的问题是列表或集合强加了它们的行为方式。 但 IEnumerable 非常通用,不仅仅是 C# 意义上的通用。 每个实现都可以在其背后。
一些建议:
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: