How do I declare a variable so that every class (*.cs) can access its content, without an instance reference?
5 Answers
In C# you cannot define true global variables (in the sense that they don't belong to any class).
This being said, the simplest approach that I know to mimic this feature consists in using a static class, as follows:
public static class Globals { public const Int32 BUFFER_SIZE = 512; // Unmodifiable public static String FILE_NAME = "Output.txt"; // Modifiable public static readonly String CODE_PREFIX = "US-"; // Unmodifiable } You can then retrieve the defined values anywhere in your code (provided it's part of the same namespace):
String code = Globals.CODE_PREFIX + value.ToString(); In order to deal with different namespaces, you can either:
- declare the
Globalsclass without including it into a specificnamespace(so that it will be placed in the global application namespace); - insert the proper using directive for retrieving the variables from another
namespace.
5 Comments
You can have static members if you want:
public static class MyStaticValues { public static bool MyStaticBool {get;set;} } 8 Comments
static example you provided can't really be considered a global var.First examine if you really need a global variable instead using it blatantly without consideration to your software architecture.
Let's assuming it passes the test. Depending on usage, Globals can be hard to debug with race conditions and many other "bad things", it's best to approach them from an angle where you're prepared to handle such bad things. So,
- Wrap all such Global variables into a single
staticclass (for manageability). - Have Properties instead of fields(='variables'). This way you have some mechanisms to address any issues with concurrent writes to Globals in the future.
The basic outline for such a class would be:
public class Globals { private static bool _expired; public static bool Expired { get { // Reads are usually simple return _expired; } set { // You can add logic here for race conditions, // or other measurements _expired = value; } } // Perhaps extend this to have Read-Modify-Write static methods // for data integrity during concurrency? Situational. } Usage from other classes (within same namespace)
// Read bool areWeAlive = Globals.Expired; // Write // past deadline Globals.Expired = true; 1 Comment
First examine if you really need a global variable instead using it blatantly without consideration to your software architecture. - The question didn't asked "should I use global variables in C#", it asked "How do I create a global variable in C#"A useful feature for this is using static
As others have said, you have to create a class for your globals:
public static class Globals { public const float PI = 3.14; } But you can import it like this in order to no longer write the class name in front of its static properties:
using static Globals; [...] Console.WriteLine("Pi is " + PI); 1 Comment
Zotta's answer is good. You can also set up global variables with your ".csproj" project file. This way, your code doesn't even need a using statement.
Specify your global variables in a static class:
public static class Globals { public const float PI = 3.14; } In your ".csproj" file, add this item group:
<ItemGroup> <Using Include="Globals" Static="true" /> </ItemGroup> Now you can use global variables in your code without any further ceremony:
Console.WriteLine("Pi is " + PI);