Application Object need to be initialized first time into Global.asax.cs application_start() method and keep alive until a IIS reset or worker process recycle not occurs.
1. The cache is thead-safe, i.e. the objects in the cache may not be thread safe, but the cache collection itself is.
e.g.
Application_Start()
{
Application["globalVar1"]="Global variable One stored here";
Gvar2= "Global Variable Two is here";
}
And this can be get anywhere in application by using
string var1 = (string)Application["globalVar1"];
public class Global : System.Web.HttpApplication
{
public static string StaticTest ="This is the original value";
. . .
If a static member is named MyStaticMember in your Global.asax file, you can use MyClass.MyStaticMember to access it from your page.
or
public static string GVar2;
For using the Gvar2 we need to simply write the className.Gvar2 // Here Global.Gvar2 will do the work for us.
reading / writing a static reference variable is not (at least not without some additional work).
So what factors you must count for choosing static -
1. Application holds variable as Object.
2. While getting the value we need to caste into the objects which it stores. Of course casting need some value.
So when we need to use some primitive variables and they are not going to changes very frequent (locking issue in case of frequent changes) we should use static variable.
Application object need to be used when we need to Hold some objects application wide.
Storing and reading a static value is faster when we compare it with the Application object because static variables do not need to look-up in a collection when you refer to them and you do not need to cast from object to a specific type.
The key reason that the Application object exists in ASP.NET is for compatibility with classic ASP code to allow easy migration of existing applications to ASP.NET.
Also, yes, static variables behave the same way regardless of where they are loaded from, and exist exactly once per app domain (unless you're talking about those labeled [ThreadStatic])
Also, yes, static variables behave the same way regardless of where they are loaded from, and exist exactly once per app domain (unless you're talking about those labeled [ThreadStatic])
No comments:
Post a Comment