'Why do we use internal classes for constants in C#?
I have seen it in many projects. Why do developers use internal classes to store the constant variables in C#?
For instance:
internal static class Constants
{
public const double Pi = 3.14159;
public const int SpeedOfLight = 300000; // km per sec.
}
Solution 1:[1]
Simply, the designer decided that this class need to be used within the same assembly. And it is not to be exposed to or accessed from any project referencing the assembly.
When you download a Nuget package, you can't access classes that are internal. The developers decided that you don't need to access these. So these values are "private" for this package.
More on access modifiers:
public :Access is not restricted.
protected :Access is limited to the containing class or types derived from the containing class.
internal: Access is limited to the current assembly.
protected internal : Access is limited to the current assembly or types derived from the containing class.
private : Access is limited to the containing type.
Solution 2:[2]
Because when constants are publicly exposed (instead of internal), the danger exists that when an outside assembly references them, it may become "out of date" when the assembly that declares them is updated with new constant values, but the referencing assembly is not re-compiled.
Say, for example, the referenced assembly containing the "updated" constant values forms part of a "plugin" architecture, where a new version of the plugin could simply be "dropped" into the referencing application's folder without recompiling and redeploying the application. Even if the application that referenced a constant from the "plugin" assembly that originally declared it, the application will not use the new updated values, and will continue using the old ones instead.
This happens because when the compiler sees a constant, it "inlines" its value into the expressions or statements that contain ("reference") it, wherever that may be.
To solve this problem, and you really want to expose a "constant" to the outside world, rather declare the symbol as public static readonly. That way, if the value is ever updated, any outside assemblies that reference it will automatically use the update value(s), even without needing a recompile.
But if you really want to use const instead, make sure it is really a constant that will never change, i.e. laws of nature, physical constants, like Pi.
Solution 3:[3]
While most answers describe the meaning of internal they are missing a part of the question I think: Why put constants in a inner class?
This is mostly because it can they desire to identify constants through something feeling like a namespace:
double myValue = Constants.Pi * 10;
Or
double myValue = OtherClass.Constants.Pi * 10;
Without the inner class they would look more like members/properties. At least when they do not use UPPER_CASING for the naming.
To make the answer complete: The internal is used to protect the constant against using them outside of the assembly.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | |
| Solution 3 | ZoolWay |
