Click here to Skip to main content
14,770,332 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Can anyone help me on global variables in c sharp and how memory is allocated to it
Updated 11-Jul-11 10:20am

Harishankar Maurya 17-Jun-13 4:25am
How to declare Global varible in a page and can acces from another page in c# without using session variable
Technically, there aren't any - everything is class based, so there is no concept of a global variable in C#

However, if you absolutely must use them, then declare a static class, and use static variables in that:
public static class Globals
   public static string MyString = "Hello";
Generally, you should try to avoid them though.
Sergey Alexandrovich Kryukov 11-Jul-11 15:54pm
True (my 5), but even this is not good enough; Singleton is more advanced and robust pattern.
Pay attention -- singleton is not the same as static class; it's better not to use static class -- please see my solution.
#realJSOP 11-Jul-11 15:56pm
I don't think Griff needs a programming lesson. The "Pay attention" part of your comment was a bit condescending.
Sergey Alexandrovich Kryukov 11-Jul-11 15:58pm
How come a person needs a protection against "a lesson", especially so experienced as Griff? He is not a kid. What's wrong with you today, John? Did not get enough sleep? Me too, so I would understand you.
#realJSOP 11-Jul-11 16:11pm
I'm sure Griff knows the benefits of a singleton. Regarding how robust it is compared to a static class, any code is only as robust as the programmer makes it. I personally like static classes. They're handy. They're easily maintainable. They don't need to be instantiated by the programmer. They work. I have no complaints.
Sergey Alexandrovich Kryukov 11-Jul-11 16:02pm
At to the lessons... we all need then. Anyway, I'm sure about myself, so I regularly take lesson no matter who is giving them to me. I think that being offended by a lesson is a sign of immaturity. And I don't know why should we speak about Griff. He can speak on his own.

#realJSOP 11-Jul-11 15:54pm
You shouldn't avoid them to the poiint that your code is making all kinds of bizarre gyrations in the name of OOP purity, though. I use 'em simply 'caiuse it pisses OOP purists off. I could tell you stories... :)
Sergey Alexandrovich Kryukov 11-Jul-11 15:57pm
This is not for purism. Singleton in not a nonsense invention, I'll tell you. It all depends on the goals of the design.
#realJSOP 11-Jul-11 16:11pm
In that comment, I wasn't talking about singletons. I was talking about static classes.
OriginalGriff 12-Jul-11 6:20am
Gentlemen, gentlemen! If you are going to fight, please take it outside! :laugh:
Singleton and static both have their place.
While I (generally) try to avoid "global" variables because they can cause so much grief, there are times when they are worth having - but in most cases they are indicative of a coder who doesn't actually know what he / she is doing.

The static / singleton debate is not new; for what it's worth, for beginners I would always recommend a static solution - it is far too easy to c*** up a singleton, or create two instances via inheritance if you don't understand what you are doing. It is a lot harder to do that with a static class as the compiler generally complains when you do. If you do get a singleton wrong it can be very difficult to understand and fix a bug based on just the symptoms! I would leave singletons until introducing Threading, when thread safety becomes a big factor - a lot easier to cope with in a singleton than a static class.
In addition it is a lot easier to explain how a static class works in term beginners can understand than to explain how the singleton pattern is functioning.
Anyone who wants to know how to declare a global in C# is clearly a beginner! :laugh:
Espen Harlinn 12-Jul-11 10:05am
Nice and simple reply, my 5
[no name] 24-Mar-12 9:02am
My 5+
Member 14369041 15-May-19 4:39am
Can I use Global variables in JS function?[^]: In C#, there are no global variables or methods as there are in some other languages. Even a program's entry point, the Main method, must be declared within a class or struct

The behavior of globals is achieved through static functions and static variables
Global? What is it? There is no such concept in .NET; I think this is because this concept is evil, so it was eliminated from the architecture. You never really need "global" variables, but static variables of class or structure can be made visible to whole application domain. If the access modifier of such variable is internal, it is visible to the part of application domain limited by the same assembly; if the access modifier is public, the variable is accessible in whole application domain from any assembly.

This is equivalent to the globally-accessed variable. However, this kind of programming should be best avoided.

If you need global access to some object unique to application domain, you can use singleton ([^]) design pattern ([^]).

Here is how:
public class MySingleton {
   private MySingleten() {} //to prevent creation of another instance
   public int value; //not static!
   public string value; //not static!

   public MySingleton Instance {
      get {
         if (fInstance == null)
            fInstance = new MySingleton(); //lazy
         return fInstance;
   } //Instance

   private static fInstance;
} //class MySingleton.

Pay attention that the Lazy evaluation pattern ([^]) is also used.

In many cases the singleton needs to be shared between different threads. In this case, locking can be conveniently implemented inside singleton class in a way transparent to the users of the singleton.

See more detailed article with good non-thread-safe and thread-safe implementations of the Singleton:[^].

Catalin Serafimescu 12-Jul-11 5:53am
A nice analysis about singletons:

Or, a more elegant and flexible way, though complex: Dependency Injection, like Unity ;)
Espen Harlinn 12-Jul-11 10:08am
Adding another framework, to solve a simple requirement?
Sergey Alexandrovich Kryukov 12-Jul-11 10:24am
Thanks for the note; I would up-vote it if you post it as your solution.
Espen Harlinn 12-Jul-11 10:06am
Good points, my 5
Sergey Alexandrovich Kryukov 12-Jul-11 10:22am
Thank you, Espen.
[no name] 24-Mar-12 8:56am
who the f*** voted down for it?...
Sergey Alexandrovich Kryukov 24-Mar-12 18:10pm
From time to time, somebody votes 1 for some 5 answers in a row, with no apparent correlation with the content, it looks like...
[no name] 24-Mar-12 8:59am
My 5+..
Sergey Alexandrovich Kryukov 24-Mar-12 18:12pm
Thank you.
Will you formally accept this answer (green button)? -- thank you.
Member 14369041 15-May-19 4:45am
Is it Possible to use global variables in JS function?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900