There is a debate in between my team members about declaration of Constants clas
ID: 644311 • Letter: T
Question
There is a debate in between my team members about declaration of Constants class. We are moving the constant variables into separate class as like below
public class Contants
{
public const string StateId = "ST";
public const string CountryId = "CI";
}
few of my team members are suggested to declare the class as sealed to avoid overriding option, and few are suggested to mark it as static to avoid instance creation of constant class.
However I preferred to have it as Sealed with static constructor, since it will help us to initialize the read-only variables in future need. Please advice this..
Explanation / Answer
It's not totally clear what your question is, but if the values are truly constant, I don't see a problem with the simple option of:
public static class LocationConstants
{
public const string StateId = "ST";
public const string CountryId = "CI";
}
Using static in the class declaration signals your intention for the purpose of this class.
Marc Gravell describes some of the potential issues with constants in this Stack Overflow answer. Only you will know if these are a problem in your codebase, but if the values could ever change, use public static readonly instead of const, else any code referring to the constants will need to be rebuilt if the values change.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.