'局部常数之间的差异'和'常数字段'在R#设置?

时间:2015-02-19 21:56:00

标签: c# resharper

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

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

那么2个设置之间有什么区别?

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

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

enter image description here

2 个答案:

答案 0 :(得分:3)

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

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
}

答案 1 :(得分:1)

这应该说清楚:

enter image description here

请注意非常明确的警告信息:

enter image description here