Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to make a global Var. C#

Tags:

c#

Is it possbile to make a variable global in C#?

like image 377
gurehbgui Avatar asked Dec 16 '22 20:12

gurehbgui


1 Answers

Well, you can make a public static variable:

public static class Globals
{
    public static string Foo;
}

However, I'd strongly urge you not to do this:

  • It becomes unclear what's using the variable
  • There's no sort of thread safety
  • It makes testing a pain (in particular if you want to parallelize tests)

I'd urge you to try very hard to design away from globals. If you could tell us more about why you think you want a global variable, we may be able to give you some advice on how to avoid it in this particular case :)

like image 187
Jon Skeet Avatar answered Jan 06 '23 15:01

Jon Skeet