Declaring constants named f () as a constant value

Scenario

I have a class for declaring string constants used around a program:

 public static class StringConstants { public const string ConstantA = "ConstantA"; public const string ConstantB = "ConstantB"; // ... } 

Essentially, it does not matter what the actual value of the constant is, how it is used for purpose and consumption. This is just to check.

The names of the constants will be clear enough, but I want to try to avoid the same string values ​​more than once.


What i would like to do

I know that nameof() is evaluated at compile time, so it's quite possible to assign a const string value to the nameof() a element.

To keep a record of these magic strings, I thought about using nameof() itself as a constant.

Same:

 public static class StringConstants { public const string ConstantA = nameof(ConstantA); public const string ConstantB = nameof(ConstantB); // ... } 

Question...

I think there is no real benefit to using nameof() , except for refactoring?

Are there any consequences for using nameof() when assigning constants?

Should I use only a gated string?

+5
source share

Source: https://habr.com/ru/post/1260584/


All Articles