.NET系列命名规则规则、系列、NET

2023-09-02 20:49:07 作者:权世界只爱你

我和我的同事一直有哪些类别应该叫讨论。

My colleague and I have been having a discussion about what Collections should be called.

例如:

类产品 - 系列 - 类产品

Class Product - Collection - Class Products

类产品 - 系列 - 类ProductCollection

Class Product - Collection - Class ProductCollection

我有一个环顾四周,看看我能看到任何指导或理由使用一个或另一个但似乎没有跳出。该框架似乎用这两种变体的例子。我可以看到的论点是有产品变量的集合类应该被称为产品,但它应该是类型ProductCollection的。

I've had a look around to see if I can see any guidelines or reasons for using one or the other but nothing seems to spring out. The framework seems to use both variants for example. The argument I can see is that a class that has a collection of products variable should be called Products but it should be of type ProductCollection.

这是正确的,如果任何?

Which is correct if any?

在同一叶片是有返回变量的命名为函数的标准。例如retVal的?

In the same vane is there a standard for the naming of return variable for a function. e.g. retVal?

我们主要是code在C#中,但我不知道这影响了我的问题。

We mainly code in C#, although I'm not sure that affects my question.

推荐答案

我要说的是,与仿制药应该很少能成为一个理由来创建自定义集合类型。但是,如果你要我要说的是, ProductCollection 将最适合的框架的命名约定。

I would say that with generics there should rarely ever be a reason to create a custom collection type. But if you must I would say that ProductCollection would best fit the naming conventions of the framework.

不过,考虑使用名单,其中,产品> 收藏<产品> 或更好,但的IList<产品> 的ICollection<产品>

Still, consider using a List<Product> or Collection<Product> or better yet IList<Product> or ICollection<Product>.

编辑: 这是响应低于MrEdmundo的评论的

在你的情况,你有两个选择。最明显的选择是使用继承这样的:

In your case you have two choices. The most obvious choice would be to use inheritance like this:

class Ball { }

class BallCollection : List<Ball>
{
    public String Color { get; set; }
    public String Material { get; set; }
}

我说,很明显,因为它的似乎的像乍一看最好的想法,但后一点还以为它变得清晰,这是不是最好的选择。如果您或Microsoft创建一个新的什么 SuperAwesomeList&LT; T&GT; ,你想用它来提高你的表现 BallCollection 类?通过继承类和不断变化的基类将有可能打破这种使用 BallCollection 为名单,其中,T&GT;

I say obvious because it seems like the best idea at first glance but after a bit of thought it becomes clear that this is not the best choice. What if you or Microsoft creates a new SuperAwesomeList<T> and you want to use that to improve the performance of your BallCollection class? It would be difficult because you are tied to the List<T> class through inheritance and changing the base class would potentially break any code that uses BallCollection as a List<T>.

那么,什么是更好的解决方案?我建议,在这种情况下,你会更好青睐的组成的超过继承。那么,什么会组成的解决方案是什么样子?

So what is the better solution? I would recommend that in this case you would be better off to favor composition over inheritance. So what would a composition-based solution look like?

class Ball { }

class BallCollection
{
    public String Color { get; set; }
    public String Material { get; set; }
    public IList<Ball> Balls { get; set; }
}

请注意,我已经宣布了属性为类型的IList&LT; T&GT; 。这意味着,你可以自由地实现使用任何类型你想,只要​​该类型实现属性的IList&LT; T&GT; 。这意味着您可以自由使用 SuperAwesomeList&LT; T&GT; 在任何时候,这使得这种类型的显著更具扩展性和更痛苦,以保持

Notice that I have declared the Balls property to be of type IList<T>. This means that you are free to implement the property using whatever type you wish as long as that type implements IList<T>. This means that you can freely use a SuperAwesomeList<T> at any point which makes this type significantly more scalable and much less painful to maintain.