正确的属性命名
在下面的示例中,哪种是命名我的“[Nn]ame”属性的正确方法? .NET 中的一切似乎都是某种形式的 Pascal 情况。使用驼峰式命名法作为属性名称会被认为是“不好的形式”吗?我阅读了 MS 命名指南,他们说使用 Pascal 案件。我真的很讨厌帕斯卡的情况,看起来一切都是这样的。
普遍认可的标准是什么(仅仅是 MS 指南)还是无关紧要?如果我在这里使用camel case是不是很糟糕? TIA。
using System;
namespace Properties {
class Program {
static void Main() {
var tester = new Tester();
tester.Name = "jmquigley";
tester.name = "another jmquigley";
tester.Show();
}
}
class Employee {
public string Name { get; set; }
public string name { get; set; }
public void Show() {
Console.WriteLine("Name = {0}", Name);
Console.WriteLine("name = {0}", name);
}
}
class Tester : Employee {
}
}
注意:我在这里仅使用继承来查看使用属性时如何处理该属性(以查看所有内容是否都像字段一样工作)。
In the contrived example below, which is the proper way to name my "[Nn]ame" property? It seems that everything in .NET is some form of Pascal case. Would it be considered "bad form" to use camel case for property names? I read the MS guidelines on naming, and they say to use Pascal
case. I really hate Pascal case and it seems like it is ALL that way.
What is the generally agreed upon standard (is it just the MS guide) or does it not matter? Am I bad if I use camel case here? TIA.
using System;
namespace Properties {
class Program {
static void Main() {
var tester = new Tester();
tester.Name = "jmquigley";
tester.name = "another jmquigley";
tester.Show();
}
}
class Employee {
public string Name { get; set; }
public string name { get; set; }
public void Show() {
Console.WriteLine("Name = {0}", Name);
Console.WriteLine("name = {0}", name);
}
}
class Tester : Employee {
}
}
NOTE: I only used inheritance here to see how a property was handled when using it (to see if everything worked just like a field).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
的,普遍认可的标准是微软指南。
您的代码在不遵循准则的情况下也可以正常运行,但如果出现以下情况,我会遵循约定:
Yes, the generally agreed-upon standard is the Microsoft guidelines.
Your code will run fine without following the guidelines, but I would follow the conventions if:
Microsoft 指南在整个 .Net Framework 中使用,并受到 StyleCop、FXCop 和 ReSharper 等工具的支持。对属性使用驼峰式大小写而不是字段只会让大多数期望相反的 C# 开发人员感到困惑。
The Microsoft guidelines are used throughout the .Net Framework, and supported by tools like StyleCop, FXCop and ReSharper. Using camel case for properties instead of fields will just confuse the majority of C# developers who expect the opposite.
一般商定的标准是帕斯卡情况。
有关该主题的指南有很多:
如果您的代码有可能被其他人看到,我建议您遵循 Pascal 大小写指南。
The general agreed upon standard is Pascal case.
There are numerous guides on the subject:
I would recommend that you follow the Pascal case guideline if there is any possibility that your code will be seen by anybody else.
驼峰式案例通常是标准的做事方式。尽管如果您正在从事个人项目,或者出于其他原因可以选择自己设定标准,那么它可能并不那么重要。
Camel case is generally the standard way of doing things. Although if you are working on a personal project, or for some other reason have the option of setting the standard yourself then it probably isn't that important.
标准的美妙之处在于有很多可供选择的标准。
坦白说,这并不重要。如果您有 ReSharper,它的默认设置将是 PascalCase Name 属性,因为它是公共属性。
The wonderful thing about standards is that there are so many to choose from.
Frankly, it doesn't matter much. If you had ReSharper, its default setting would be to PascalCase the Name property, because it is a public property.
当然,您可以随心所欲地编写代码...只要确定,如果您不将所有内容都大写,那么其他所有查看您的代码的 .NET 开发人员都会认为它看起来很奇怪。
此外,任何使用 C# 进行编程的工作都可能会要求您使用大写字母。
Of course you can code however you wish... Just be certain that if you don't capitalize everything, every other .NET developer who looks at your code will think it looks strange.
Also, any job you get programming in C# will likely want you to use capitals as well.
Pascal 大小写在 .NET 世界中占主导地位。
没有什么会强迫您在自己的代码中使用 Pascal 大小写,但您最终会得到 pascal 大小写(框架类型)和驼峰大小写(您自己的类型)的混合。在我看来,不一致比必须使用帕斯卡大小写更糟糕。
此外,如果您是一个更大团队的一员,或者某个程序员同事将来可能需要维护您的代码,那么这是遵守现状的另一个好理由。
Pascal case is dominant in the .NET world.
Nothing forces you to use Pascal case for your own code, but you will end up with a mix of pascal case (framework types) and camel case (your own types). Inconsistency is worse than having to use pascal case, imo.
Additionally, if you're working as part of a larger team or if some fellow programmer might need to maintain your code in the future, that is another good reason to conform to the status quo.
标准方式是属性的帕斯卡大小写和字段的驼峰式大小写并带有下划线。
Standard way is Pascal Case for Properties and camel case with an underscore for fields.
嗯,我在野外看到的方式是属性通常是帕斯卡大小写。 Pascal 大小写通常用于属性、方法和类。
关于约定,需要记住的重要一点是,它是为了缓解两个人之间的沟通障碍,而不是个人喜好。当我最初编写 Java 后第一次开始使用 C# 时,转向 Pascal 用例的世界是非常奇怪的,但回想起来,我应该更早地采用这些约定。
Well, the way I've seen it in the wild is properties are usually Pascal case. Pascal case tends to be used for properties, methods, and classes.
The important thing to remember about conventions is it's about easing the communication barriers between two people as opposed to personal preferences. Moving over to a world of Pascal case was very weird when I first started C# after initially writing Java, but looking back, I should have adopted the conventions earlier.