Reputation: 35853
If I implement a Singleton as follows, putting into App_Code, will the instance be reclaimed by GC after each round-trip HTTP requests? Or it'll still persist in the runtime? Thanks for any help.
public sealed class Singleton
{
static readonly Singleton instance=new Singleton();
static Singleton()
{
}
Singleton()
{
}
public static Singleton Instance
{
get
{
return instance;
}
}
}
Upvotes: 1
Views: 215
Reputation: 7266
No, not on each HTTP request. To prove that, I added a readonly timestamp to the class:
public readonly DateTime Timestamp = DateTime.Now;
and then referenced it on a page in the project. It remained the same on each refresh.
You mention the GC. Remember that there is no guarantee of when the GC will reclaim an object (even GC.Collect() I don't think guarantees an object will be reclaimed). But I don't think that that is the gist of your question.
Basically, yes your singleton will act as a singleton, at least until the application is recycled.
Upvotes: 1