Click here to Skip to main content
15,921,577 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
thanks..?
Posted
Updated 11-Jul-11 9:20am
v2

 
Share this answer
 
Comments
Harishankar Maurya 17-Jun-13 4:25am    
How to declare Global varible in a page and can acces from another page in c# asp.net 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";
   }
...
   Console.WriteLine(Globals.MyString); 
Generally, you should try to avoid them though.
 
Share this answer
 
Comments
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.
--SA
#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.
--SA
#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.

Cheers,
--SA
http://msdn.microsoft.com/en-us/library/ms173109.aspx[^]: 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
 
Share this answer
 
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 (http://en.wikipedia.org/wiki/Singleton_pattern[^]) design pattern (http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29[^]).

Here is how:
C#
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 (http://en.wikipedia.org/wiki/Lazy_evaluation[^]) 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: http://csharpindepth.com/Articles/General/Singleton.aspx[^].

—SA
 
Share this answer
 
v5
Comments
Catalin Serafimescu 12-Jul-11 5:53am    
A nice analysis about singletons: http://www.yoda.arachsys.com/csharp/singleton.html

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.
--SA
Espen Harlinn 12-Jul-11 10:06am    
Good points, my 5
Sergey Alexandrovich Kryukov 12-Jul-11 10:22am    
Thank you, Espen.
--SA

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