![]() |
When are static members garbage collected?
I have an object that I am using in my ASP.net app. I only want one instance of this object available to all of my pages.
Here is a sample public class statictes private statictest() {} // static onl private static string data = null public static string Dat ge if(data != null) return data data = "INIT" return data se data = value I am assuming that this private static member will no be garbage collected as long as the application is running. Is this correct? Would it be better to store it in cache (I should not expire until application end, and it does not have any dependencies) Thank |
Re: When are static members garbage collected?
statics are GC'd when the appdomain is unloaded. (life of the application).
special note: your sample code is not thread safe. -- bruce (sqlwork.com) "J" <anonymous@discussions.microsoft.com> wrote in message news:B028C369-111F-4661-AFB4-5B31DF1DA3F7@microsoft.com... > I have an object that I am using in my ASP.net app. I only want one instance of this object available to all of my pages. > > Here is a sample: > > > public class statictest > { > private statictest() {} // static only > > private static string data = null; > > public static string Data > { > get > { > if(data != null) return data; > data = "INIT"; > return data; > } > set > { > data = value; > } > } > } > > I am assuming that this private static member will no be garbage collected as long as the application is running. Is this correct? Would it be better to store it in cache (I should not expire until application end, and it does not have any dependencies)? > > Thanks > |
| All times are GMT. The time now is 04:45 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.