C# does not support global variables. You'd have to toss those into a struct or a class. However, assuming your "global variables" are actually in a class and your struct is actually instantiated and assigned to a property on a class, the advantage to using a struct would be that you can group your variables logically and pass them around easily. Imagine if all the variables, constants, functions, and so on in the .Net Framework were all global rather than nicely nested in namespaces, classes, and structs. You'd end up with something horrible and impossible to navigate through (e.g., VB6).
[edited] Actually, if you use a struct, a struct is passed by value unless you use the "ref" keyword everywhere. What this means is that if you use any mutable (changeable) members and change the value, the original structure is not updated when passed as a parameter (without "ref") or defined as a property of a class. For example,
internal static class Preferences
{
public struct Globals
{
public string Option1 = "foo";
public string Option2 = "bar";
}
private static Globals globals;
public static Globals Globals { get { return globals; } }
}
If later you change the code like so:
Preferences.Globals.Option1 = "baz"
- the Globals structure is not changed.
Just best to keep these as members of a class. If constants are all you use, just define them as constants of a class. If you need mutable options, use a singleton class. More information about good singleton patterns can be found here on Code Project or on
MSDN[
^].
But there's also an entire set of APIs for managing settings like this in the .NET Framework that Visual Studio exposes through the project properties / settings tab. It has the added benefit of being able to merge different user contexts if so desired as well as designer support.