
You want to use global variables in your program written in the C# programming language. Although global variables can cause certain problems in software development, many programs can benefit from them if used in a controlled manner. Here we see how you can use global variables in programs developed for the .NET Framework in the C# language, first testing an example and then discussing issues related to global variables.
The static keyword in the C# language describes a member such as a field, property or method that is part of a type, not an instance of the type. The static keyword was mainly chosen for historical reasons as it was used in C and C++. The program here contains two files: first a file that contains the global variables in a public static class, and then the Program.cs file that uses the global class.
Global variables class [GlobalVar.cs, C#]
/// <summary>
/// Contains global variables for project.
/// </summary>
public static class GlobalVar
{
/// <summary>
/// Global variable that is constant.
/// </summary>
public const string GlobalString = "Important Text";
/// <summary>
/// Static value protected by access routine.
/// </summary>
static int _globalValue;
/// <summary>
/// Access routine for global variable.
/// </summary>
public static int GlobalValue
{
get
{
return _globalValue;
}
set
{
_globalValue = value;
}
}
/// <summary>
/// Global static field.
/// </summary>
public static bool GlobalBoolean;
}
Program that uses global variables [Program.cs, C#]
using System;
class Program
{
static void Main()
{
// Write global constant string.
Console.WriteLine(GlobalVar.GlobalString);
// Set global integer.
GlobalVar.GlobalValue = 400;
// Set global boolean.
GlobalVar.GlobalBoolean = true;
// Write the two previous values.
Console.WriteLine(GlobalVar.GlobalValue);
Console.WriteLine(GlobalVar.GlobalBoolean);
}
}
Output
Important Text
400
True
Overview. The example is divided into three parts. The first part shows a class that contains four members: a constant global value, a property with a backing store, and a public static field that is of Boolean type. The second part of the example shows the Main entry point in the Program.cs file.
Const string member. The public const string with the identifier GlobalString is an excellent use of a global variable in the C# language. This field is constant, which means you must assign its value inline with its declaration at the class declaration space. The public access modifier on the field makes it accessible throughout your program.
Global property member. The GlobalValue member is a function type containing a property get accessor and set accessor. This is an access routine on its backing store, which is the reference to the data it points to. The _globalValue field is modified through the set accessor; it is accessed through the get accessor.
Property ExamplesGlobal field. Finally, the global variable class contains a global field of Boolean type. The bool type in the C# language aliases the System.Boolean type, which inherits from the System.ValueType struct. This bool field could cause problems in your program because it does not use an access routine.

Experts in software development consider global variables to be used most effectively when accessed through special methods called access routines. You can use access routines in nearly any programming language. These methods provide another layer of abstraction over how you use the global variables. In the example, the property accessor (get) is considered an accessor routine. See Code Complete, 2nd Edition by Steve McConnell; page 340.
Code Complete (Book Review)Global variables and static variables in C# programs can introduce problems when using multithreading, which is used extensively in many ASP.NET websites. An access routine could provide locking using the 'lock' expression in the C# language. This is also a problem in many Windows Forms applications that use BackgroundWorker threads.
Global Variables Example Lock StatementWe saw how you can use global variables in the C# programming language by using a static class with constants, properties and public fields. You can also define methods with parameters to access the static variables. We looked an example of using these global variable types, and then discussed issues relating to global variables and threading. Finally, we emphasized the usage of access routines.
Method Tips