C# Global Variable

Class: shapes

Global variables are useful. But they can lead to certain problems that are hard to debug. Global variables can cause certain problems in software development. But many programs can benefit from them if used in a controlled manner.

Example

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++.

Static

Next:We see two files: a file that contains the global variables in a public static class, and Program.cs, which 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
Const keyword

The public const string with the identifier GlobalString is an excellent use of a global variable. This field is constant. You must assign its value inline with its declaration at the class declaration space.

Tip:The public access modifier on the field makes it accessible throughout your program.

Property

The GlobalValue member is a function type containing a property get accessor and set accessor. This is an access routine of a backing store. The backing store is a reference field of type int.

Note:The _globalValue field is modified through the set accessor. It is accessed through the get accessor.

PropertyBool keyword

Global 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.

Tip:This bool field could cause problems in your program because it does not use an access routine.

Access routines

Code Complete

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.

Tip:These methods provide another layer of abstraction over how you use the global variables.

And:In the example, the property accessor (get) is an accessor routine. Please see Code Complete by Steve McConnell, page 340.

Code Complete

Threads

Lock keyword

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-statement.

Global VariablesLock

Also:This is a problem in many Windows Forms applications that use BackgroundWorker threads.

BackgroundWorker

Summary

The C# programming language

We used global variables by adding 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.

Finally:We discussed issues relating to global variables and threading. We emphasized the usage of access routines.


C#: Method