Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote