gpt4 book ai didi

c# - R# 设置中 'Local constants' 和 'Constant fields' 之间的差异?

转载 作者:行者123 更新时间:2023-11-30 15:26:48 27 4
gpt4 key购买 nike

如果您打开 R# 选项并转到代码编辑> C# > 命名样式,则有 2 个设置与我非常相似。本地常量和常量字段(私有(private))。一种是 lowerCaseCamel,另一种是 UpperCamelCase。

我注意到这一点是因为之前 R# 建议我将所有方法内变量更改为以大写字母开头的常量,但现在它告诉我将它们全部设置为小写(我做了很多调整R# 并尝试对错误等实现一些解决方法,但我认为我在本节中没有更改任何内容)。

那么这两种设置有什么不同呢?

此外,由于我们处于最重要的位置,每个设置的 R# 默认设置是什么?对于如何设置每个设置是否有非基于意见的(例如 Microsoft 规范)?

C# naming convention for constants? 中的说明确实与 Should a local const variable start with Upper or lower casing 中所述的内容有些冲突,所以我正在寻找基于 R#/Microsoft 建议的答案,而不是基于意见的答案。

enter image description here

最佳答案

本地常量对于方法或方法体而言是本地的(如构造函数、属性 getter 等)。我个人主要在单元测试中使用这些,很少在应用程序代码中使用。这些就像变量一样,只是它们不能改变,我相信 Microsoft 和 R# 的约定都是驼峰式。

public void SomeMethod()
{
const string someText = "hi";
const int someInt = 6;
const bool someBool = false;
// code that operates using the above constants
// not available outside of the method body
}

常量字段在类或结构上可用,并且可以由多个方法/其他类和协作者访问。我认为 R# 已经为这些做了 PascalCase。不确定 Microsoft 的官方约定是什么,但在 Java 中我认为这些应该是 UPPER_WITH_UNDERSCORES。

public class SomeClass
{
public const string SomeText = "Hi"; // accesible everywhere
internal const int SomeInt = 6; // accessible within assembly
private const bool SomeBool = false; // accessible within class/struct only
}

关于c# - R# 设置中 'Local constants' 和 'Constant fields' 之间的差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28617735/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com