In all applications, there are always some constant values that are used.
I don't like hardcoding these values in my application in each code as it reduces maintainability significantly.
I have 2 options:
Create an internal Constants class within your target class and add public const values such as
internal static class Constants { public const string IdAttribute = "id"; }
or
- Create a Resource file (.resx) and store my constant values and formulas there.
I have used the second approach in my application here. One issue I had with using a resource file was that I could not use .NET 4.0 Optional Parameter feature with them.
e.g. the below doesn't work:
public void DoSomething(string id = Resources.Constants.Id)
{
// ..
}
The error was that the value of Id is not determined at runtime.
Basically, which way would you use and recommend to manage your constants for each class? why?
My current approach is that if I have constants that should be used inside one class only, I create a static Constants class inside that class. If my constants are to be used in more than one class or project, I'll put them in a resource file e.g. PageUrls.resx.
Thanks,