. Advertisement .
..3..
. Advertisement .
..4..
While it isn’t officially supported, you can still create a global variable in C# with other components of this programming language. In this post, we will show you how.
Global Variables In C#
In C#, there is no such thing as a global variable. This is an object-oriented programming language, and variables that can be accessed from anywhere violate its principles.
However, this concept can come in handy in many situations. The only solution you can try is to use a static class to mimic global variables. They are certainly not the same thing, but it is as close as you can get.
Declare A Global Variable In C# With A Static Class
In C#, we have static and non-static classes. The main difference is that you can’t instantiate a static class, meaning you can’t create a new variable of this class type with the new operator. And since a static class has no install variable, its members can only be accessed through the class name.
Static classes are often used in C# to form convenient containers for methods that don’t set or get internal instance fields. These methods, therefore, only need input parameters to operate and don’t retrieve or store data unique to any instance of the class.
Here is an example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GlobalVarDemo
{
static class ITTutoria
{
public static int ranking = 2;
public static string language = "Python";
public static string HelloWorld()
{
return "Welcome to ITTutoria.net!";
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(ITTutoria.HelloWorld());
Console.WriteLine("Site's top language: " + ITTutoria.language);
Console.WriteLine("Site's original ranking: " + ITTutoria.ranking);
ITTutoria.ranking = 1;
Console.WriteLine("Site's new ranking: " + ITTutoria.ranking);
}
}
}
Output:
Welcome to ITTutoria.net!
Site's top language: Python
Site's original ranking: 2
Site's new ranking: 1
In the program above, first we create a static class called ITTutoria
. In its body, we declare two static variables and a function. They are members of the class.
As you can see from the function Main()
, we can directly call the method HelloWorld()
of this class without having to create an instance first. Then we print the returned value with the function Console.WriteLine()
. In a similar manner, we access the variable language of the ITTutoria
class like a global variable as well.
The second part of the example demonstrates how you can not just access but also change members of static classes in C#. The first time we access the variable ranking, it returns the exact same value we have declared in its static class. However, we can only directly set it to a new value as well, which can be verified by a new call.
Declare A Global Variable In C# With Static Members
Remember that you can also create static members from a non-static class. They can act like global variables as well, meaning you can access them from anywhere in your program without using an instance of that class.
The example above can be rewritten without using a static class:
public class ITTutoria
{
public static int ranking = 2;
public static string language = "Python";
public static string HelloWorld()
{
return "Welcome to ITTutoria.net!";
}
}
You can use the members of this class (ITTutoria.ranking
and ITTutoria.language
) without modifying the previous Main()
function.
Conclusion
Remember that using a global variable in C# has many pitfalls. You should only use them in certain situations, such as when parameters or instances aren’t practical.Using the methods introduced in this article requires extra attention. Be cautious when using multithreading, for example. Make sure you have some kind of locking and allow only one thread to access your global variable at a time. And if you want to know how to use global variables in C++, read this guide.
Leave a comment