Are Static Properties in Static Classes Safe?

Last post 07-03-2009 3:42 PM by JeffreyABecker. 3 replies.

Sort Posts:

  • Are Static Properties in Static Classes Safe?

    07-02-2009, 3:22 AM
    • Member
      31 point Member
    • ksome
    • Member since 03-26-2009, 8:12 AM
    • Posts 56

    When my Page_Load event handler is called, I load Session data into static properties in a static class so that the rest of the classes in the app can have easy access to the data.  Is this safe to do when there will be multiple, concurrent users of the application?

    It seems to me that it depends on how web applications are run.  Is the web app really a DLL that is called by IIS?  In this case, all users would share the same DLL and static variables so what I doing would be a problem.  If on the other hand, the web app is run like a real app - EXE - then each user's instance of the app is a separate process and will have its own static properties.

    Are one of these cases right?  If not, what is going on?  And, is it safe to use static properties?

  • Re: Are Static Properties in Static Classes Safe?

    07-02-2009, 3:29 AM
    Answer
    • All-Star
      30,502 point All-Star
    • HeartattacK
    • Member since 01-08-2007, 5:53 PM
    • Dhaka, Bangladesh
    • Posts 3,292
    • Moderator

    DO NOT USE STATIC VARIABLES FOR USER SPECIFIC DATA.

    Your concern is very correct. Session is per user, static variables are not. Changing the static variable for one user will change it for all users. 

    All that glitters is gold-
    Only shooting stars break the mold.

    Read my blog: www.heartysoft.com

    Tell me what tutorials / articles / videos you want to see on my site.
  • Re: Are Static Properties in Static Classes Safe?

    07-02-2009, 3:35 AM
    Answer
    • All-Star
      46,043 point All-Star
    • jimmy q
    • Member since 11-02-2006, 9:01 AM
    • Australia
    • Posts 3,176
    • Moderator
      TrustedFriends-MVPs


    A simple explanation is static is like global so any change will affect other users who are relying on this variable.

    Static variables are loaded in the appdomain so all processes/threads within it share the same value.

    If you are after a session specific container then use ASPNET Session object.

    If you want to use it like a global field then you can use static but remember to make it threadsafe

  • Re: Are Static Properties in Static Classes Safe?

    07-03-2009, 3:42 PM
    Answer
    • Star
      14,260 point Star
    • JeffreyABecker
    • Member since 10-04-2004, 4:27 AM
    • Philadelphia, PA
    • Posts 2,911

    ksome:
    It seems to me that it depends on how web applications are run.  Is the web app really a DLL that is called by IIS?  In this case, all users would share the same DLL and static variables so what I doing would be a problem.  If on the other hand, the web app is run like a real app - EXE - then each user's instance of the app is a separate process and will have its own static properties.

    Your analysis is dead on.  It would be 'safe' in a single user context, but not in a multi-user context.  Maybe you could set up some sort of cache which used methods instead?

Page 1 of 1 (4 items)