C# 代码分析不喜欢受保护的静态 s_Foo(CA1709、CA1707)

发布于 2024-07-18 05:55:24 字数 601 浏览 4 评论 0原文

我通常在 private 字段前面添加一个 m_,在 static 成员之前添加一个 s_

使用像这样的代码,

protected static readonly Random s_Random = new Random ();

我通过 VS2008 的代码分析得到以下警告:

  • CA1709: Microsoft.Naming : 通过将成员名称“Bar.s_Random”中的“s”更改为“S”来更正其大小写。
  • CA1707:Microsoft.Naming:删除成员名称“Bar.s_Random”中的下划线。

如何解决这个问题? 我应该简单地删除 s_ 吗? 或者为此警告添加全局抑制?

编辑:我的公司缺乏编码标准,因此由我来为我的代码定义它们。 (是的,我知道...)

如果您认为一般情况下应该删除 s_,我会很高兴您能提供官方来源。

I usually add an m_ in front of private fields and an s_ before static members.

With a code like

protected static readonly Random s_Random = new Random ();

I get the following warnings by VS2008's Code Analysis:

  • CA1709: Microsoft.Naming : Correct the casing of 's' in member name 'Bar.s_Random' by changing it to 'S'.
  • CA1707: Microsoft.Naming : Remove the underscores from member name 'Bar.s_Random'.

How to resolve this issue? Should I simply remove the s_? Or add a global suppression for this warning?

Edit: My company lacks coding standards, so it's up to me to define them for my code. (Yea I know...)

If you think s_ should be removed in general, I'd be glad if you could provide official sources.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

怀中猫帐中妖 2024-07-25 05:55:25

您没有遵循 Microsoft 的 .NET 命名约定,该约定告诉您不要在内容前面添加任何前缀。 如果这确实是您想要的,请添加抑制。 否则,请按照指南删除 s_ 和其他类似的前缀。

来自类型成员的名称
“字段名称”部分:“不要使用字段名称前缀。例如,不要使用 g_ 或 s_ 来区分静态字段和非静态字段。”

You are not following Microsoft's .NET naming convention that tells you not to prefix stuff with anything. If this is really what you want, add a suppression. Otherwise, follow the guideline by getting rid of s_ and other similar prefixes.

From Names of Type Members:
"Names of Fields" Section: "Do not use a prefix for field names. For example, do not use g_ or s_ to distinguish static versus non-static fields."

死开点丶别碍眼 2024-07-25 05:55:25
  1. 卸载项目
  2. 右键单击卸载的项目=> 编辑 csproj
  3. 将 RunCodeAnalysis 设置为 false
  4. 保存并重新加载项目
  1. Unload the project
  2. Right click on the unloaded project => Edit csproj
  3. Make RunCodeAnalysis to false
  4. Save and reload project
浪荡不羁 2024-07-25 05:55:25

取决于你想要什么。

如果您的公司政策是在静态成员前加上 s_ 前缀,那么您应该禁止显示警告,甚至添加您自己的规则。

否则,将其修复为 Microsoft 标准并调用您的成员Random

Depends on what you want.

If your company policy is to prefix static members with s_ then you should suppress the warning and even add your own rule.

Otherwise fix it to Microsoft's standards and call your member Random.

哀由 2024-07-25 05:55:25

m_ 是一个旧的命名标准。 新的约定不遵循这种匈牙利符号。

m_ is an old standard for naming. Newer conventions are to not follow this Hungarian notation.

挽袖吟 2024-07-25 05:55:25

这取决于你想如何解决它。 忽略它,并保留您自己的命名约定,或遵循 Microsoft 标准。 就我个人而言,我不为我的变量使用任何前缀(因此在这种情况下将是“随机”而不是“s_Random”),所以我会选择后者,但如果你真的对此感到满意,那么没有人强迫你改变。

It's up to you how you want to resolve it. Ignore it, and keep your own naming convention, or follow up the Microsoft standard. Personally, I do not use any prefix for my variables (so that would be 'random' instead of 's_Random' in this case) so I would go with the latter, but if you're really comfortable with this, then nobody forces you to change.

带刺的爱情 2024-07-25 05:55:25
  • CA1709:受保护的不是私有的,需要大写
  • CA1707:下划线不符合 ms 命名约定
  • CA1709 : protected is not private and needs to be upper case
  • CA1707 : underscores are not according to ms naming convention
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文