15

Someone once said:

a "global variable" is really a variable you create to simply "hold some information" because your object model is weak, and you haven't found a "true purpose" for the variable to exist. Global variables are almost always a sign of a larger architectural deformity.

That might be true. But I don't know of any good example of any big and good program made without global variables, and certainly not used as little as above suggested. Scope is the actual key. You can say in a program with only one class that its parameters are not global vars. But they are.

Anyway...

I'm still grasping the concept of singleton and, as far as I can tell, they actually make no sense in C#. Also I get the feeling that when having a global state can't be avoided we still should avoid simply using a public class full of static properties:

So, if not singletons nor a public class, what should we do to have global vars in C#?

And when are we supposed to use them? Assuming they most likely can't be avoided. Ever.

And why should we avoid using a static class or static in general? If that's indeed the case for C#.

In one sentence: What are the best practices on using global variables in Csharp?

Community
  • 1
  • 1
cregox
  • 17,674
  • 15
  • 85
  • 116
  • 2
    Technically there is no such thing as a global variable in C#, they can of course be global to a class and made public, essentially achieving the same thing, but scoped down a bit. – Ed S. Apr 12 '12 at 18:37
  • Why don't singletons make sense in C#? – Katie Kilian Apr 12 '12 at 18:37
  • 3
    Although is well written and in a correct way, I think this question doesn't apply for this site? – gbianchi Apr 12 '12 at 18:40
  • 1
    @gbianchi: I agree with you, but I'd +100 this if I could. I can't wait to see what the stack elites have to say about it. – Daniel Szabo Apr 12 '12 at 18:41
  • Doesn't this belong on http://programmers.stackexchange.com ? – emd Apr 12 '12 at 18:45
  • Closed because it's rhetorical, huh? Was it because of the disclaimer? I thought feeding and redundancy were welcomed here. @emd though I am interested in the general concepts and I may be a little confused about them, here I'm asking specifically about C#. So I'd guess it'd belong here. – cregox Apr 12 '12 at 19:25
  • @CharlieKilian in C++ a programmer here uses it only for getting access to meta classes, which is just not in C# concept. Maybe it does have an usage in which static parameters couldn't be used, I just don't know enough to tell which that usage would be. I'd expect an answer to contain a point for that. Maybe this was closed to be "overly broad", after all... But then again, people closing my questions could be kind enough to comment on why. – cregox Apr 12 '12 at 19:32
  • @gbianchi could you suggest me a SEN site to which I could apply it? – cregox Apr 12 '12 at 19:34
  • Guys, (from Robaticus to ErikPhilips) I *hope* my last edit is enough to show this was intended to be a "real question". – cregox Apr 12 '12 at 19:43
  • @Cawas In C#, singletons are a design pattern that mean whenever you obtain an instance of an object, you are assured to always get the same instance. See here for an example: http://msdn.microsoft.com/en-us/library/ff650316.aspx It's been too long since I did any C++ development, so perhaps it's the same concept. Regardless, they are, in fact, used in C#. – Katie Kilian Apr 12 '12 at 20:15
  • @CharlieKilian to me adding what you said to the **context** on that link is simply saying "*here's how we do global vars in C# using a single object as an excuse that it ain't a global var*". What else would a "global point of access to a class with only one instance" be used for? If so, then saying "singletons are actually the way MSDN advices for global vars", pointing to that link and explaining why in there they never explicitly say "global var" would be a great answer to me. – cregox Apr 12 '12 at 21:08
  • @Cawas Ahh, I think we are talking over each other. I thought you were saying that singletons *never* make sense in C#, but it sounds like you are actually saying that singletons don't make sense as a replacement for global variables for the OP's problem. *That* I completely agree with. Sorry for the confusion. – Katie Kilian Apr 12 '12 at 21:24
  • @CharlieKilian yes, perfect! :) But I was just guessing they're not good as global var replacement, and I'm glad you do agree. So I still would like to see any example in which singletons do have a good use because I still don't get what they *are* used for in C#. – cregox Apr 12 '12 at 21:44
  • @Cawas I would use a singleton instead of a static class if I needed to be able to mock it for another class during unit testing. Instantiated classes are much easier to mock. That is just one example off the top of my head. – Katie Kilian Apr 12 '12 at 21:52
  • @CharlieKilian well, I must admit there's a lot of new concepts there for me! But seems like singletons are not the best way even for mock testing, at least in C#: http://stackoverflow.com/questions/2050892/how-to-mock-a-static-singleton – cregox Apr 12 '12 at 22:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10026/discussion-between-charlie-kilian-and-cawas) – Katie Kilian Apr 12 '12 at 22:31

1 Answers1

6

It's all a matter of context.

If you can determine the ambient variables that you need for a computation, you can wrap that computation in a bigger context where those global-looking variables have a narrower scope to inhabit (they're now local to the context).

This is better because now you can have multiple instantiations of your context, that should work independently and not interfere with each other. It's also called a reentrant context.

Jordão
  • 55,340
  • 13
  • 112
  • 144
  • So you're saying the whole [global variable](http://en.wikipedia.org/wiki/Global_variable) concept simply doesn't exist in C# and if there is something that copies it behavior in C# (like singletons) it should **never** be used? If that's it, could you provide an example to save a configuration value for a constant that will be the same through the whole application? Let's say I want to have in just one place a configuration for the application version and the company web site. And a third kind of global var that's not constant, the current application language. – cregox Apr 12 '12 at 21:51
  • Yes, they exists, and it's up to you to control them or otherwise limit their scope. Application version and company website seem like _metadata_ that should be just scoped to the _assembly_ (with an assembly attribute); the application language can depend on the currently logged in user, so it might pertain to a _request_, or a _session_ scope. You get the picture... – Jordão Apr 12 '12 at 22:46
  • Well, none of that sounds like global var to me - yet they all seem to act like one. I think I got the grasp of it... – cregox Apr 12 '12 at 23:32
  • Whenever you have things like that, think about what will happen if you have multiple _instances_ of the scope at the same time. Multiple user sessions shouldn't share the same language settings, for example. – Jordão Apr 12 '12 at 23:49
  • Multiple instances of the scope in a traditional "global vars scheme" wouldn't affect each other, unless it's the same program running all instances - because traditionally each instance would be the copied binary running. But all that's food for another subject. And since you brought the language thing again, just wanted to mention I hate it when a program on my machine sets the language based on a OS request or session. It should be a setting in which I choose. Many machines here in Brazil, the OS is in portuguese, but I like the software I run to be always in english nevertheless. – cregox Apr 13 '12 at 13:12