Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices to using global variables in C# [closed]

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?

like image 360
cregox Avatar asked Apr 12 '12 18:04

cregox


People also ask

Is it good practice to use global variables in C?

Master C and Embedded C Programming- Learn as you go It can not be limited to some parts of the program. Using global variables causes very tight coupling of code. Using global variables causes namespace pollution. This may lead to unnecessarily reassigning a global value.

What is the best way to declare and access a global variable?

The clean, reliable way to declare and define global variables is to use a header file to contain an extern declaration of the variable. The header is included by the one source file that defines the variable and by all the source files that reference the variable.

What can I use instead of global variables in C?

In a scenario, where you need one central global point of access across the codebase, singletons are a good alternative for global variables. We can call a singleton as a lazily initialized global class which is useful when you have large objects — memory allocation can be deferred till when it's actually needed.

What is the main problem with using global variables?

Pointers to globals are even more evil. The main problem with using global variables is that they create implicit couplings among various pieces of the program (various routines might set or modify a variable, while several more routines might read it).


1 Answers

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.

like image 166
Jordão Avatar answered Sep 28 '22 06:09

Jordão