正确的属性命名

发布于 2024-10-20 04:05:28 字数 858 浏览 4 评论 0原文

在下面的示例中,哪种是命名我的“[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 技术交流群。

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

发布评论

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

评论(9

娇纵 2024-10-27 04:05:28

普遍同意的是什么
标准(只是MS指南)

的,普遍认可的标准是微软指南。

还是不重要?

您的代码在不遵循准则的情况下也可以正常运行,但如果出现以下情况,我会遵循约定:

  • 您是团队的一员
  • 您正在为雇主生成代码
  • 您正在生成开源代码
  • 您希望能够在线阅读其他人的代码容易地。
  • 您将来想要执行上述任何操作

What is the generally agreed upon
standard (is it just the MS guide)

Yes, the generally agreed-upon standard is the Microsoft guidelines.

or does it not matter?

Your code will run fine without following the guidelines, but I would follow the conventions if:

  • You are part of a team
  • You are producing code for an employer
  • You are producing open-source code
  • You want to be able to read others' code online more easily.
  • You want to do any of the above in the future
小糖芽 2024-10-27 04:05:28

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.

单身情人 2024-10-27 04:05:28

一般商定的标准是帕斯卡情况。

有关该主题的指南有很多:

  1. Microsoft 命名指南
  2. IDesign C# 编码标准
  3. Phillips C# 编码标准
  4. C# 编码风格指南
  5. CLS 规范

如果您的代码有可能被其他人看到,我建议您遵循 Pascal 大小写指南。

The general agreed upon standard is Pascal case.

There are numerous guides on the subject:

  1. Microsoft Naming Guidlines
  2. IDesign C# Coding Standards
  3. Phillips C# Coding Standard
  4. C# Coding Style Guid
  5. CLS Specification

I would recommend that you follow the Pascal case guideline if there is any possibility that your code will be seen by anybody else.

池木 2024-10-27 04:05:28

驼峰式案例通常是标准的做事方式。尽管如果您正在从事个人项目,或者出于其他原因可以选择自己设定标准,那么它可能并不那么重要。

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.

空‖城人不在 2024-10-27 04:05:28

标准的美妙之处在于有很多可供选择的标准。

坦白说,这并不重要。如果您有 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.

メ斷腸人バ 2024-10-27 04:05:28

当然,您可以随心所欲地编写代码...只要确定,如果您不将所有内容都大写,那么其他所有查看您的代码的 .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.

始于初秋 2024-10-27 04:05:28

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.

何其悲哀 2024-10-27 04:05:28

标准方式是属性的帕斯卡大小写和字段的驼峰式大小写并带有下划线。

public string Name { get; }
private string _name;

Standard way is Pascal Case for Properties and camel case with an underscore for fields.

public string Name { get; }
private string _name;
羞稚 2024-10-27 04:05:28

嗯,我在野外看到的方式是属性通常是帕斯卡大小写。 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.

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