TheEdge TheEdge - 8 months ago 36
ASP.NET (C#) Question

Where is this singleton saving its information?

I am looking at the code presented in this SO answer

The code works exactly as advertised but what I don't understand is how the singleton is storing its static members across requests e.g:

private static string host = null;

Is it by virtue of the fact that the FirstRequestInitialisation class is static that it is saving it in the storage for the app?


Your assertion that it is because the FirstRequestInitialisation class is static is correct. Being a static class, it remains in memory for the lifetime of the application domain in which your program resides. This means whenever the 'FirstRequestInitialisation` class is referenced, the same instance is returned across the application, in the same way a static member is shared across all instances of the parent type.

See Static Classes and Static Methods article on the MSDN for more info.