C# Code

public class TestClass
{
private static string Str = string.Empty;
public TestClass()
{
if (string.IsNullOrEmpty(Str))
{
Str = "TestStr";
}
}
public static string GetValue()
{
return Str;
}
}

//以上代码中Str是一个需要在程序初始化时根据实际情况赋值并作为静态属性存放于内存中,方便程序运行过程当中取值

当通过TestClass.GetValue()方法取值时,结果却不是"TestStr"而为"";通过调试追踪发现,public TestClass()根本没有被执行,这里还不能很好的理解其原因。

posted on 2015-01-08 23:53  Byron_2015  阅读(213)  评论(1编辑  收藏  举报