1

Tried to find an answer but I didn't find any information...

I've build a full property based on this following example:

private static Foo _foo;
public static Foo foo {
get 
{
   if (_foo!= null) 
      return _foo;

   //else do some logic and fill _foo;
   _foo = ....;
}};

Question is, when will _foo be null according to life span on the page? On first load of course it will be null but when will it be null again? On recycle? on iisreset? or page reload?

This property sits on an class lib (external dll)

Ziv Weissman
  • 4,400
  • 3
  • 28
  • 61
  • 1
    The accepted answer in the linked question is the answer to your question - though it has nothing to do with whether or not the variable is `private` or not. – Rob Dec 20 '15 at 07:56
  • No, this is a question about lifecycles in MVC , not about C# static. – H H Dec 20 '15 at 09:06
  • Side note, the proper naming for the property is `public static Foo Foo {...}` – H H Dec 20 '15 at 09:13

1 Answers1

2

when will _foo be null according to life span on the page? On first load of course it will be null but when will it be null again? On recycle? on iisreset? or page reload?

It is not bound to a Page, in MVC instance data is relative to a Request and static data is bound to the Application instance.

It will be null each time the Application is restarted, ie on an IIS Recycle or a Reset.

That makes static data rather dubious in Server applications. It can be used as a simple form of caching but be aware that this doesn't 'scale out'. Each server will have its own copy.

H H
  • 263,252
  • 30
  • 330
  • 514