3

If you open up R# options and go to Code Editing> C# > Naming Style there are 2 settings that seem very similar to me. Local constants and Constant Fields (private). One is to lowerCaseCamel and the other UpperCamelCase.

I noticed this because previously R# suggested I change all of my in-method variables to constants starting with a capital, however now it is telling me to set them all to lower case (I had done a lot of tweaking around with R# and trying to implement some workarounds to a bug etc, however I do not believe I changed anything in this section).

So what are the differences between the 2 settings?

Also since we are on the top of it, what is the R# default setting for each and is there a non-opinion based (e.g. Microsoft specification) for how to set each setting?

What is stated in C# naming convention for constants? does appear to conflict a little with what is stated in Should a local const variable start with Upper or lower casing, so I'm looking for an answer based on what R#/Microsoft recommendations not an opinion based answer.

enter image description here

Community
  • 1
  • 1
Adrian773
  • 473
  • 11
  • 22

2 Answers2

3

Local constants are local to a method or method body (like a constructor, property getter, etc). I personally use these mostly in unit tests, rarely in application code. These are just like variables except that they cannot be mutated and I believe the convention for both Microsoft and R# is 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
}

Constant fields are available on a class or struct, and can be accessed by multiple methods / other classes and collaborators. I think R# has you do PascalCase for these. Not sure what the official Microsoft convention is, but in Java I think these are supposed to be 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
}
danludwig
  • 46,965
  • 25
  • 159
  • 237
1

This should make it clear:

enter image description here

Notice the very, very clear warning message:

enter image description here

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170