帮我命名这个属性 (C#)
想象一下我创建了这个类:
public class ColoredPolygon
{
public Point[] Vertices { get; set; }
public Brush Brush { get; set; }
}
在 Visual Studio 内部不难看出哪个“Brush”是类,哪个是属性。 但有些事情告诉我,根据属性的类别来命名属性并不是最佳实践。 你会如何命名这个属性?
更新 还有一个关于命名的问题:如何命名数组? 用复数还是用单数?
Imagine I created this class:
public class ColoredPolygon
{
public Point[] Vertices { get; set; }
public Brush Brush { get; set; }
}
Inside Visual Studio isn't difficult to see which "Brush" is the class and which is the property. But something tells me that is not a best practice to name a property after its class. How would you name this property?
Update One more question about naming: How do you name Arrays? With the plural or with singular?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我将其称为
Brush
。 开发类库的设计指南实际上说:(在有关命名 Type 成员的部分中)。 该语句后面的文本建议对 Enum 类型的属性执行此操作,但您可以在 .NET 框架中找到针对其他类型对象的大量此方法示例,例如
Brush
和Pen
类的Color
属性。不用担心,如果
Brush
是您的属性的描述性名称,您应该使用该名称,特别是因为它甚至会遵循您在框架中看到的命名模式。I would call it
Brush
. The Design Guidelines for Developing Class Libraries actually says:(in the section about naming Type members). The text following the statement suggests to do this for properties that are of Enum types, but you can find plenty of examples of this approach for other types of objects in the .NET framework, for instance the
Brush
andColor
properties of thePen
class.Nothing to worry about, if
Brush
is a descriptive name for your property you should use that name, especially since it would even follow the naming pattern that you can see within the framework.FillBrush 是一个很好的描述性名称(我已经投票了),但说实话,在它真正成为一个真正的问题之前,不要担心它。 就您而言,它可能不是最好的名称,但如果它是最好的名称,我会保留它,直到上下文另有规定为止。 我有几个例子,其中类和属性的名称相同,但由于属性通常由变量名称(或
this.
)限定,所以我不太担心它。我发现以下内容非常可读:
FillBrush is a good, descriptive name (and I've upvoted) but, honestly, don't worry about it until it actually becomes a real problem. In your case, it may not be the best name, but if it were the best name, I'd keep it until the context dictated otherwise. I have a few examples where the name of the class and the property is the same, but since the property usually is qualified by a variable name (or
this.
), I don't worry too much about it.I find the following pretty readable:
FillBrush
怎么样?How about
FillBrush
?除了另一个答案中提到的
FillBrush
之外,您还可以考虑添加一种仅设置填充Color
的方法,这样用户就不必只构建画笔设置纯色填充。As well as
FillBrush
, mentioned in another answer, you might consider adding a way to set just the fillColor
, so that the user won't have to construct a brush just to set a solid color fill.关于 Fredrik 的建议(Brush Brush)以及 MS 的命名建议,您可以考虑为属性提供与其类型相同的名称,但您不能总是这样做。 例如,如果您在类中定义了一个名为
Something
的公共枚举,则该类不能同时具有名为Something
的属性。您可以拥有一个名为
Brush
的属性,大概是因为Brush
是在不同的命名空间/类中定义的,然后编译器足够智能来找出哪个Brush
代码>你的意思是。Regarding Fredrik's suggestion (Brush Brush) and also MS's naming suggestions, you may consider giving a property the same name as its type, but you cannot always do it. If for example you have a public enum called
Something
defined in a class, then that class cannot also have a property calledSomething
.You can have a property called
Brush
, presumably becauseBrush
is defined in a different namespace/class and the compiler is then smart enough to figure out whichBrush
you mean.