我看到了一段代码,其中原开发人员似乎尝试使用静态字符串来缓存静态类中的值。
据我所见,这样做不起作用,因为您无法实例化
public static class GetStringFromSomeProcess
{
private static string theAnswer;
public static string GetString
{
get
{
if(theAnswer == null)
{
theAnswer = GoGetTheAnswerFromALongRunningProcess();
}
return theAnswer;
}
}
}
据我所见,这样做不起作用,因为您无法实例化
GetStringFromSomeProcess
类,每次使用GetString
时都会调用GoGetTheAnswerFromALongRunningProcess
。我有什么遗漏的吗?
GetStringFromSomeProcess
?在第一次调用GetString
后,theAnswer
将不再为空,因此具有可怕名称的方法将不再被调用。 - Mong Zhu