Reputation: 4006
I have done some searching, but haven't found much. At the moment I am using:
class GlobalArrays
{
public static string[] words = { "Easy", "Medium", "Hard" };
public static Color[] backColors = { Color.LightGreen, Color.Orange, Color.Red };
}
Which works well, but I don't know if it is the correct way to do it. I've seen global variables done like this:
static class GlobalVars
{
const string SOMETHING = "LOL";
}
Which is supposedly the Microsoft approved way of declaring namespace level constants, but when I tried that with arrays, it threw an error, saying they could only be of type string
.
static class GlobalArrays
{
public const string[] words = { "Easy", "Medium", "Hard" };
public const Color[] backColors = { Color.LightGreen, Color.Orange, Color.Red };
}
The above code won't compile, and says they can only be initialized with null, because they are of a type that is not string
.
Upvotes: 0
Views: 1670
Reputation: 4268
According to the compiler:
A const field of a reference type other than string can only be initialized with null.
I think this is about as close as you're going to get:
private static readonly string[] words = { "Easy", "Medium", "Hard" };
public static IReadOnlyCollection<string> Words
{
get
{
return Array.AsReadOnly(words);
}
}
Upvotes: 3