is Global variable good or bad in c#.net?
-
is Global variable good or bad in c#.net? If i declare Global variables instant of singleton variables its good or bad for application?? Thanks Raj
As a general practice, global variables are bad. This compromises the security of the application, as well as eliminating any chance of data-hiding through the use of objects and variable passing. Now, this of course depends on the type of information and the use of the program. You may want to use a static global variable for things like links or connection information etc. This means that the variable cannot be changed at any point of from anywhere else in the program though. It will exist as a static reference value. What are you trying to accomplish with the global variable?
-
is Global variable good or bad in c#.net? If i declare Global variables instant of singleton variables its good or bad for application?? Thanks Raj
I'd say it is pretty meaningless to say this or that programming practice is inherently good or bad. The only proper answer is always "it depends". That said, I'm not really sure what you mean by "global variable". C# does not have them. You seem to mean instance members of a singleton. From the language's point of view that's just like any other instance member, the singleton after all is a pattern you can implement with C#, but not a concept that the C# language itself "is aware" of. In general, if you are using the object-oriented paradigm, it is considered bad practice not to encapsulate state. Therefore, you should expose the state through properties (or methods in a few cases). These may be read-only or read-write, as long as you perform validation to guarantee object state remains valid at all times. What is your motivation for wanting to use a "global variable"? There may be many ways to accomplish what you seek, such as using static members (which is the closest thing to global variables you can find in C#), thread data, application state (in an asp.net context) or other mechanisms.
-
As a general practice, global variables are bad. This compromises the security of the application, as well as eliminating any chance of data-hiding through the use of objects and variable passing. Now, this of course depends on the type of information and the use of the program. You may want to use a static global variable for things like links or connection information etc. This means that the variable cannot be changed at any point of from anywhere else in the program though. It will exist as a static reference value. What are you trying to accomplish with the global variable?
Eric (eD) wrote:
As a general practice, global variables are bad.
Myth. Global variables should generally be avoided when possible (and used as a last resort), but they're not "bad", and there are times when you simply must use one to accomplish some tasks. Besides that, global variables *technically* aren't even possible in .Net.
Eric (eD) wrote:
You may want to use a static global variable for things like links or connection information etc. This means that the variable cannot be changed at any point of from anywhere else in the program though.
Not quite. Static members in an object exist for the lifetime of the application, and can be changed from anywhere (and by any other object) if they have the appropriate visibility. In fact, it's common practice to create a static class for just that purpose. In many ways, using static class members can be fraught with as much - if not more than - "global" variables, but like globals, they're pretty damn handy in a pinch, and you shouldn't hesitate to use them if the architecture demands it.
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001