Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is the difference b/w Global Variable, Static Variable , Instant Variable

and

1. Life Time of Global Variable
2. Life Time of Static Variable
3. Life Time of Instant Variable


Thanks in advance
Posted
Updated 21-Feb-13 23:56pm
v2
Comments
Richard MacCutchan 22-Feb-13 6:06am    
You can find this information quite easily by picking up your reference manual or a bit of Google searching.
Balu Balaji 22-Feb-13 6:08am    
Global Variable Scope if i declared it as a Public then we can access through out the Project
is this correct or not

"Global Variable Scope if i declared it as a Public then we can access through out the Project
is this correct or not"


Yes - and no.
More "No" in actual fact.

C# has no concept of global variables, they do not exist. Everything in C# is contained within a class of some form, in some way. So adding public does not make a variable a global variable in the sense that declaring it outside of a class does in C++, or declaring it outside a method or function, but in a module does in VB.
What declaring a variable as public does is:
1) Annoy people who have to maintain your code - you should keep variables (or fields as they are better known) private and expose any access to them via properties instead.
2) Make the variable available outside the class. This is not the same as a global variable, because it still requires a class instance to access it:
C#
public class MyClass
   {
   public int MyInt = 666;
   }
...
   MyClass mc = new MyClass();
   Console.WriteLine(mc.MyInt);


So in answer to your three questions:
1) Nonexistent - C# does not support global variables.
2) From creation of the variable to the end of the application.
3) Assuming you mean "Instant Variable" to mean one declared within a method, it depends on what happens to it. If it is a value type, then it's lifetime is that of the method itself (or less if it goes out of scope). If it is a reference type, then it will become available for disposal when it goes out of scope, assuming you do not pass a reference to it out of the method. The actual lifetime of any reference type is indeterminate as it wholly depends on the Garbage Collector execution cycle if it is not explicitly Disposed.

[edit]dunno what happened there - code block all wierd... - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
Balu Balaji 22-Feb-13 6:56am    
Thanks a Lot so in VB.NET also There is NO Global Variables i think
 
Share this answer
 
Comments
Balu Balaji 25-Feb-13 0:01am    
Thank you very much............
Shubh Agrahari 25-Feb-13 1:00am    
always be ready to help......

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