Monday, February 11, 2008

State Management with ASP .NET 2.0 - 2

In the previous post about state management I talked about the ViewState collection. Today I will write about the Application collection.

2. Application State Collection

The Application collection is an object collection that maps strings to objects. The unique aspect of the Application collection is that it is a global variable such that it is shared throughout the entire ASP .NET application. The Application collection has the same life span as the ASP .NET process and dies or is cleared when the process is killed or restarted.

The use of the Application collection is to store variables that need to be retained across different pages and shared amongst different users and sessions. An example of something that might be stored in the Application collection would be a counter of some sort that is shared amongst pages and sessions.

The Application collection is used like any collection in .NET. To store something (in code behind):

int n = 0;
Application["keyID"] = n;


To retrieve a variable from the collection:

int x = (int)Application["keyID"];

No comments: