There is no such thing as global object in .NET. And this is very good, because global objects are potentially unsafe and indicate bad programming style. Object can be shared in one or another context.
However, this is not applicable to constants, and also for
static immutable objects (which play the role of constants in cases where constant is not allowed by syntax, for example, if you want to have an immutable array of something, or any other object which is initialized only once and cannot be modified).
As constants cannot be modified, no one carries if they are accessible in all contexts. However, nothing needs to be "global". Everything should have its context. Say, top-level type with
public
access modifier can be accessed from any assembly in your process, and
internal
(default for top-level type) will make the top-level type accessible in just your assembly.
You did not specify the scope you want to work with, so let's assume this is the whole assembly.
Now, let's define constant integers. It actually depends on the purpose. This is the most compact way:
internal enum MyValue {
Something = 75,
SomethingElse =90,
}
In this approach, each constant has its name. This is more compact then defining a class:
internal static class MyDefinitionSet {
internal static const Something = 75;
internal static const SomethingElse = 90;
}
In addition to clarity and simplicity, enumerations have important benefit: you can enumerate them, even though in not 100% straightforward way. Please see my article on the topic where I explain the background and number of techniques, starting from some well-known ones:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[
^].
The major problem is: you did not really explain what you want to do with those integers. Maybe you don't need their names, need just a list. You can have just a list, or both. You need to make the list readonly, but is should better be just the array (which of course implements
System.Generic.Collections.List<int>
:
internal static class MyDefinitionSet {
internal static readonly int[] MyIntegers = new int[] {75, 90, };
}
Depending on what you want, the combination of the approaches also can make a lot of sense:
internal static class MyDefinitionSet {
internal static readonly int[] MyIntegers = new int[] {
MyValue.Something,
MyValue.SomethingElse,
};
}
—SA