Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8068533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:43:16+00:00 2026-06-05T12:43:16+00:00

I plane to use static variables instead of Application state in ASP.NET and am

  • 0

I plane to use static variables instead of Application state in ASP.NET and am wondering if this is correct approach:

[Global.asax.cs]

...

public class Global : System.Web.HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup

    }

    ...

    private static Dictionary<string, object> cacheItems = new Dictionary<string, object>();
    private static object locker = new object();

    public static Dictionary<string, object> CacheItems
    {
        get
        {
            lock (locker)
            {
                return cacheItems;
            }
        }

        set
        {
            lock (locker)
            {
                cacheItems = value;
            }
        }
    }

    public static void RemoveCacheItem(string key)
    {
        cacheItems.Remove(key);
    }

    ...
}

As you can see I use automatically created Global.asax (and code behind) file. I’ve added some static variables and methods. I can use them after in this manner:

[some .cs file]
foreach(KeyValuePair<string, object> dictItem in Global.CacheItems)
{
    ...

Is this the right way or I should create new class instead of existing Global? If I should create new class how can I do that and where?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:43:17+00:00Added an answer on June 5, 2026 at 12:43 pm

    What Microsoft says

    ASP.NET includes application state primarily for compatibility with
    classic ASP so that it is easier to migrate existing applications to
    ASP.NET. It is recommended that you store data in static members of
    the application class instead of in the Application object. This
    increases performance because you can access a static variable faster
    than you can access an item in the Application dictionary.

    reference : http://support.microsoft.com/default.aspx?scid=kb;en-us;Q312607

    My experience

    The main different between static variables and Application state, is that the Application state is the same across all threads and pools, but the static is the same only per pool.

    After new tests I see that the Application state variables are the same as static variables, and they just reference to a static variable on the application, and they just exist for compatibility reasons as Microsoft says

    If you have 4 pools running your site (web garden) then you have 4 sets of different static memory.

    Your code

    About your code, you have bug for the way you try to access your dictionary data, and you will going to have errors in real web. This part of the code is lock the variable of the full Dictionary but not lock the change you going to make when you use it.

    // this is not enough to manipulate your data !
    public static Dictionary<string, object> CacheItems
    {
        get { lock (locker) { return cacheItems; } }
        set { lock (locker) { cacheItems = value; } }
    }
    

    The correct approach is to lock all actions of add/remove until you done, for example.

    private static Dictionary<string, object> cacheItems = new Dictionary<string, object>();
    private static object locker = new object();
    public Dictionary<string, object> CacheItems
    {
        get{ return cacheItems; }
        set{ cacheItems = value; }
    }
    
    SomeFunction()
    {
        ...
        lock(locker)
        {
            CacheItems["VariableName"] = SomeObject;
        }
        ...
    }
    

    From the other hand when you manipulate data on application state you need to use the global lock of it Application.Lock(); and Application.UnLock(); for example

    Application.Lock();
    Application["PageRequestCount"] = ((int)Application["PageRequestCount"]) + 1;
    Application.UnLock();
    

    To close with some conclusion:

    Avoid Application state and just use static variables on your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I plan to use dom4j DOM Document as a static cache in an application
A colleague of mine told me that I should never use static variables because
I'm unsure how to use the static method in this program. I am coding
Today I released a small asp.net beta web application which allows internal staff to
I typically place global compile-time variables (like constants that i use such as avogadro's
Is it possible in C# to use UTF-32 characters not in Plane 0 as
If I do a query (I plan to use SDS.P) against the global catalog,
I am using mod_perl for my web application. Currently, I plan to use a
Is there a standard method I can use in place of this custom method?
I often do this kind of mistake, instead of: Toast.makeText(this, getString(R.string.string_id), Toast.LENGTH_SHORT).show(); i do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.