Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.43/5 (4 votes)
See more:
give Defintion for me staic variable and global variable in asp.net?
Posted
Updated 17-Dec-17 20:34pm
Comments
Maarten Kools 31-Jul-13 9:29am    
Global variables do not exist in .NET
Sergey Alexandrovich Kryukov 31-Jul-13 10:29am    
Even static variables don't exist; there are only static members...
Please see my answer...
—SA
Sergey Alexandrovich Kryukov 31-Jul-13 10:29am    
It's not in ASP.NET. It's in .NET. Both do not exist, and it's good.
—SA
Laxmidhar tatwa technologies 18-Dec-17 9:24am    
Dear friends all are correct .Here the scope of the members depend on the access specifier and also the members may static or non static

First of all, forget ASP.NET. The scope of the question is .NET or CLR, nothing else. It's good to know what it is:
http://en.wikipedia.org/wiki/Common_Language_Runtime[^],
http://en.wikipedia.org/wiki/.NET_framework[^]. :-)

Now, both things you mentioned do not exist in .NET, and this is very good. All variable are only stack objects (local variables). Do you know how stack works? If not, you need to learn it, it's a separate topic. They are not allowed to be static.

There are only static members of the types. Some are always static (such as enumeration members), but usually they are either static or non-static (instance members). When you defined a structure or a class, you can create instances of them (objects), unless these types are non-static themselves. And you can define static or non-static members like fields, properties and even instances. Each instance (non-static) member is different and independent for each instance, and can be accessed only by an instance, using some instance members. As to the static member, it exist only one per whole class. In this sense, they are functionally nearly equivalent to "custom" global variables. But they are not global variables: they are safer and easier to use.

Now, there are also static methods and static types. Static types are simple: they just have only static members and come with the static keyword used to prevent creating an instance. And static methods are those which have no access to the instance (which is passes as a hidden "this" parameter). Further consideration of static vs instance method is beyond out topic, but I only want to say that static methods are often very needed. But static fields, properties and event instances should be avoided in most cases. Usually, it's possible to avoid them at all; and such programs are the best. In rare cases, they can be used, but with care.

And it's better not to use them freely, but to use them via the singleton pattern, which itself should be used with care. Please see:
http://en.wikipedia.org/wiki/Singleton_pattern[^],
http://csharpindepth.com/Articles/General/Singleton.aspx[^].

I saw many very bad implementation of singleton, but the one referenced above is good. Read to understand why.

—SA
 
Share this answer
 
aspnet-static-variable.html[^]

global-variables-aspnet[^]

Check this links for your answer..
 
Share this answer
 
They are both in memory for the entire lifetime of the program. The variable that is declared static only has scope in the file in which it is declared where as the variable declared without static can be accessed from other files using an extern declaration.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-Jul-13 10:30am    
Not exactly. They both don't exist in .NET. There are only static members of types.
And, by the way, even the static members don't have to exist during the lifetime of the application; this is pretty delicate matter.
—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