C# Global Variables, Fields and Functions
How to define a static class to make "globals" accessible from anywhere
What is a "Global" Variable?
A global variable is a variable accessible anywhere, for
example a field "counter" type integer.
The global variable can be accessed
from any function or class within the namespace.
Does C# support Global Variables?
C# is an object-oriented programming (OOP) language and does not support global variables directly. The solution is to add a static class containing the global variables. Using a global variable violates the OOP concept a bit, but can be very useful in certain circumstances.
Be aware, that using global variables is not the answer to all questions and should only be used if using instances/parameters is not practical! You should also be careful when using multithreading, e.g. a background task. Make sure, that only one thread has access to the global (static) variable at a time - or add some kind of lock routine - to avoid conflicts.
What is the difference between a "class" and a "static class"?
The "static" keyword means the variable is part
of the type and not an instance of the type!
The
following sample should illustrate the difference between a
class and a
static class:
- "a" is an instance of the class "Bread". The field "counter" can be accessed via the instance "a" only (a.counter).
- "Butter" is a static (global) class. The static field "counter" can be accessed directly via the type (Butter.counter).
using System;
using System.Collections.Generic;
using
System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace
GlobalVarDemo
{
class
Bread
{
public int counter;
}
static class Butter
{
public static
int counter;
}
class
Program
{
static void Main(string[] args)
{
// instance of class "Butter" required
Bread a = new Bread();
a.counter = 5;
Console.WriteLine("Bread: " + a.counter);
// no instance required!
Butter.counter = 5;
Console.WriteLine("Butter: " + Butter.counter);
}
}
}
Please note, that you need to add the static keyword before class and type!
How to define a "static class" to hold global Variables, Fields and Functions
Let's define a simple static class:
static class Globals
{
// global int
public
static int counter;
//
global function
public static string HelloWorld()
{
return "Hello World";
}
// global int using
get/set
static int _getsetcounter;
public static int getsetcounter
{
set { _getsetcounter = value; }
get { return _getsetcounter; }
}
}
The class "Globals" can be used anywhere without using an instance:
Globals.counter = 10;
int localcounter = Globals.counter;
string somestring = Globals.HelloWorld();
Globals.counter2
= 30;
int localcounter2 = Globals.counter2;
Sample to demonstrate the use of Global Variables (static class):
Sourcecode:
using System;
using System.Collections.Generic;
using
System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace
GlobalVarDemo
{
// static
class to hold global variables, etc.
static class
Globals
{
// global int
public static int counter;
// global function
public static string HelloWorld()
{
return
"Hello World";
}
}
class SomeClass
{
public void DoIt()
{
// the global counter was set to 10 in Program::Main
// output global counter
Console.WriteLine("Counter SomeClass: " + Globals.counter);
// increase the global counter by 1
Globals.counter += 1;
// output global counter
Console.WriteLine("Counter SomeClass: " + Globals.counter);
}
}
class Program
{
static void Main(string[] args)
{
// set the global counter to 10
Globals.counter = 10;
// output global counter
Console.WriteLine("Counter Main: " + Globals.counter);
// we can access the global variables from any
class/function
SomeClass someinstance = new SomeClass();
someinstance.DoIt();
// the global counter was increased by 1 in Someclass::DoIt
// output global counter
Console.WriteLine("Counter Main: " + Globals.counter);
// using a static function works the same way,
e.g:
// Console.WriteLine("HelloWorld: " + Globals.HelloWorld());
}
}
}
Output:
Counter Main: 10
Counter DoSomething: 10
Counter
DoSomething: 11
Counter Main: 11
HelloWorld: Hello World
The counter is set to 10 in the Progam::Main function and increased by one in an instance of SomeClass::DoIt.