帮我命名这个属性 (C#)

发布于 2024-07-22 15:52:33 字数 318 浏览 7 评论 0原文

想象一下我创建了这个类:

 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 技术交流群。

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

发布评论

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

评论(5

余罪 2024-07-29 15:52:33

我将其称为Brush开发类库的设计指南实际上说:

考虑给予相同的属性
名称作为其类型

(在有关命名 Type 成员的部分中)。 该语句后面的文本建议对 Enum 类型的属性执行此操作,但您可以在 .NET 框架中找到针对其他类型对象的大量此方法示例,例如 BrushPen 类的 Color 属性。

不用担心,如果 Brush 是您的属性的描述性名称,您应该使用该名称,特别是因为它甚至会遵循您在框架中看到的命名模式。

I would call it Brush. The Design Guidelines for Developing Class Libraries actually says:

Consider giving a property the same
name as its type

(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 and Color properties of the Pen 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.

瘫痪情歌 2024-07-29 15:52:33

FillBrush 是一个很好的描述性名称(我已经投票了),但说实话,在它真正成为一个真正的问题之前,不要担心它。 就您而言,它可能不是最好的名称,但如果它最好的名称,我会保留它,直到上下文另有规定为止。 我有几个例子,其中类和属性的名称相同,但由于属性通常由变量名称(或 this.)限定,所以我不太担心它。

我发现以下内容非常可读:

var poly = new ColoredPolygon();
poly.Brush = Brushes.Green;

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:

var poly = new ColoredPolygon();
poly.Brush = Brushes.Green;
梦里人 2024-07-29 15:52:33

FillBrush 怎么样?

How about FillBrush?

泼猴你往哪里跑 2024-07-29 15:52:33

除了另一个答案中提到的 FillBrush 之外,您还可以考虑添加一种仅设置填充 Color 的方法,这样用户就不必只构建画笔设置纯色填充。

As well as FillBrush, mentioned in another answer, you might consider adding a way to set just the fill Color, so that the user won't have to construct a brush just to set a solid color fill.

何止钟意 2024-07-29 15:52:33

关于 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 called Something.

You can have a property called Brush, presumably because Brush is defined in a different namespace/class and the compiler is then smart enough to figure out which Brush you mean.

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