I am working on a new project and I have noticed some code that I am not sure is true. The names and values I am using to demonstrate the question are fake.
public class MyConsts //Should it be static?
{
public const string MyConst1 = "a";
public const string MyConst2 = "b";
public const string MyConst3 = "c";
public const string MyConst4 = "d";
....
}
For my logic this class (that contains only consts values) should be static, so no option to initialize it, which has no sense, am I correct?
EDIT: I was writing the code blind so I have confused the order of string and const - and because it wasn't the target of my question I've fixed this.
Yes, it makes sense for it to be static. That signifies your intention, prevents clients from even declaring a variable of that type, etc.
You'll need to move the const
modifier before the type part though:
public const string MyConst1 = "a";
...
If the values could ever change, consider using public static readonly
fields instead of const
though - otherwise the value will be baked into any code which refers to the constants, which means you need to rebuild any client code if the values change.
(Another option is to make the constants internal
instead of public
.)
See more on this question at Stackoverflow