Home
Search
Global Variable ExamplesUse a global variable, implemented as public static property. Store globals in a static class.
C#
This page was last reviewed on Dec 15, 2021.
Global variables. These are useful. But they can lead to problems that are hard to debug. Global variables can cause errors in software development.
Many programs can benefit from global variables if used in a controlled manner. We use the static keyword. A separate static class can also be used.
Static
class
An example. The static keyword describes something that is part of a type, not an instance of the type. This word was chosen for historical reasons—it was used in C and C++.
Info We see the GlobalVar class, which contains the global variables in a static class, and Program, which uses the global class.
Const GlobalString is a public global variable. You must assign its value inline with its declaration at the class declaration space.
Property GlobalValue is a public static property with get and set accessors. This is an access routine of a backing store.
Property
Field A global field could cause problems in your program because it does not use an access routine.
using System; /// <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; } 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 2 previous values. Console.WriteLine(GlobalVar.GlobalValue); Console.WriteLine(GlobalVar.GlobalBoolean); } }
Important Text 400 True
Access routines. Global variables are 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.
Threads. Static fields can introduce problems when using multithreading, which is used extensively in websites. An access routine could provide locking using the lock-statement.
Global Variables
Lock
Also This is a problem in many Windows Forms applications that use BackgroundWorker threads.
BackgroundWorker
A summary. 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.
Parameters
Public, private
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 15, 2021 (edit link).
Home
Changes
© 2007-2023 Sam Allen.