0

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.
}
Ivar
  • 6,138
  • 12
  • 49
  • 61
satya
  • 1
  • 2
  • The class being internal doesn't have anything to do with constants. There's no relation between the two. Also, classes in C# are internal by default (the ones declared within a namespace). – Nasreddine Jul 06 '16 at 09:54
  • Do you know what the `internal` keyword is and why its used? Making the class `internal` is not forced and personally I'd say that having a `Constants` class filled with const values is not a good design choice. – Jite Jul 06 '16 at 09:54
  • 1
    Possible duplicate of [Practical uses for the "internal" keyword in C#](http://stackoverflow.com/questions/165719/practical-uses-for-the-internal-keyword-in-c-sharp) – Kahbazi Jul 06 '16 at 09:54
  • So everybody uses the same value for the same constants. You don't want some people using 3.14 for Pi and others using 3.14159 for Pi. – jdweng Jul 06 '16 at 09:55

3 Answers3

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.

Community
  • 1
  • 1
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
1

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.

0

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.

ZoolWay
  • 5,411
  • 6
  • 42
  • 76