When are static members garbage collected?

G

Guest

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
 
B

bruce barker

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 said:
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)?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top