Possible to make a global Var. C#
Is it possbile to make a variable globa开发者_运维知识库l in C#?
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 :)
Not directly, you could either use a static class with a static property (not recommonded) or employ a mean to create a singleton. The better way would be to use some sort of dependecy injection to supply the values.
No, It is not. The analogue/workaround is a public static variable of some class. HTH
Not really. You can make a new class, call it anything you want, make it static, and have a static public property/variable on that.
Why do you want to do this? Is this a constant?
You can also use (if it is a constant value) the appsettings node in your web/app config file.
You can make a public class with a public static variable...but it really isn't a good programming practice to do it.
Not directly, you could either use a static class with a static property (strongly not recommended) or employ a mean to create a singleton (not recommended). The better way would be to use some sort of dependecy injection to supply the values (recommended).
精彩评论