Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Can anyone help me on global variables in c sharp and how memory is allocated to it
thanks..?
Posted 11-Jul-11 10:18am
Edited 11-Jul-11 10:20am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
Harishankar maurya at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
SAKryukov at 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
John Simmons / outlaw programmer at 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.
SAKryukov at 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
John Simmons / outlaw programmer at 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.
SAKryukov at 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
John Simmons / outlaw programmer at 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... :)
SAKryukov at 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.
--SA
John Simmons / outlaw programmer at 11-Jul-11 16:11pm
   
In that comment, I wasn't talking about singletons. I was talking about static classes.
OriginalGriff at 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 at 12-Jul-11 10:05am
   
Nice and simple reply, my 5
onurag19 at 24-Mar-12 9:02am
   
My 5+
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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:
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
  Permalink  
v5
Comments
Catalin Serafimescu at 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 at 12-Jul-11 10:08am
   
Adding another framework, to solve a simple requirement?
SAKryukov at 12-Jul-11 10:24am
   
Thanks for the note; I would up-vote it if you post it as your solution.
--SA
Espen Harlinn at 12-Jul-11 10:06am
   
Good points, my 5
SAKryukov at 12-Jul-11 10:22am
   
Thank you, Espen.
--SA
onurag19 at 24-Mar-12 8:56am
   
who the f*** voted down for it?...
SAKryukov at 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...
--SA
onurag19 at 24-Mar-12 8:59am
   
My 5+..
SAKryukov at 24-Mar-12 18:12pm
   
Thank you.
Will you formally accept this answer (green button)? -- thank you.
--SA

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

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,192
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 9 Aug 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100