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 doesn't matter what the actual value of the constant is, as it used when assigning and consuming. It is just for checking against.
The constant names will be fairly self-explanatory, but I want to try and avoid using the same string values more than once.
What I would like to do
I know that nameof()
is evaluated at compile-time, so it is entirely possible to assign the const string
's value to the nameof()
a member.
In order to save writing these magic strings out, I have thought about using the nameof()
the constant itself.
Like so:
public static class StringConstants
{
public const string ConstantA = nameof(ConstantA);
public const string ConstantB = nameof(ConstantB);
// ...
}
Question...
I guess there is no real benefit of using the nameof()
, other than for refactoring?
Are there any implications to using nameof()
when assigning constants?
Should I stick to just using a hard-coded string?
var c = nameof(c);
– Vinson