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 8317075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:30:20+00:00 2026-06-08T21:30:20+00:00

in a .net web app is there something special about .aspx pages and the

  • 0

in a .net web app is there something special about .aspx pages and the c# code behind pages that changes the behaviour of static variables.

i have a large number of application pages that were developed elsewhere and there is a common pattern running thru them where what i think should be an instance variable is declared as a static variable.

a more detailed statement of the question would be: if i have two web sessions a and b running on the same iis server in the same application pool, if a accesses the page in question and sets static variable x to value1 and then b accesses the same page and sets static variable x to value 2, my understanding is that value1 has been replaced by value 2. my dilemma is that this pattern is used repeatedly in the code, at a high level the code appears to work. the conclusion is that it is either luck (timing as in session a has abandoned the need for the variable before session b hits it) or there is something else going on.

i am open to suggestions whether this is a c# nuance or a developer bug.

  • 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-08T21:30:21+00:00Added an answer on June 8, 2026 at 9:30 pm

    Static properties/fields are fine in web applications as long as they are used for shared data which can acceptably disappear at any time, such as when an app pool recycles.

    That said, their values are indeed shared inside an ASP.Net application unless they have a segregated backing mechanism, like Session.

    Example

    public static int UserId = 10; // BAD! everyone gets/sets this field's value
    
    // BAD! everyone gets/sets this property's implicit backing value
    public static int UserId {
         get;
         set;
    }
    
    // This case is fine; just a shortcut to avoid instantiating an object.
    // The backing value is segregated by other means, in this case, Session.
    public static int UserId{
        get{
            return (int)HttpContext.Current.Session["UserId"];
        }
    }
    
    // While I would question doing work inside a property getter, the fact that 
    // it is  static won't cause an issue; every call retrieves data from a 
    // database, not from a single memory location.
    public static int UserId{
        get{
            // return some value from database
        }
    }
    

    You may not see an issue until traffic is significant. Suppose a page retrieves a value, puts it in a static variable, uses it once, then completes execution. If the page executes quickly, there is only a very small (but dangerous!) window of overlap that you may not see unless the timing is right and/or traffic is high enough.

    This can lead to hard-to-diagnose bugs, because they are dependent on timing and you probably will not see them when testing by yourself on your local machine.

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

Sidebar

Related Questions

Why are the code-behind pages for an ASP.NET web application locked at run time?
We're creating a new consumer/public-facing ASP.Net web app. There are two concerns: --Use cookie
There is a ASP.NET MVC 2 web app, lets call it myapp.com. I want
I am just about to launch my ASP.NET MVC3 web app to production, however,
I have an .net 2.0 C# client app that has a web service reference
I have an ASP.NET 4 web app that is using the Entity Framework to
Is there any ToDo app that have .NET api for server side adding tasks.
I am relatively new in C# and ASP.NET MVC. There is something unusual that
I've inherited a VB.net project that generates 2 DLLS: one for the web app,
In my asp.net Web app. I have a change password form that is not

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.