Gui*_*rme -2 c# stack-overflow static properties set
当我尝试设置静态属性时,我收到了堆栈溢出异常.
public static class StaticTest
{
static string stringToSet
{
get
{
return stringToSet;
}
set
{
stringToSet = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,在其他课程中:
public void setStaticProperty()
{
StaticTest.stringToSet = "Hello World"; // StackOverflow exception here
}
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
set
{
stringToSet = value;
}
Run Code Online (Sandbox Code Playgroud)
你的setter中有无限递归(并且就此而言是getter),因为它自称,因此StackOverflow.
如果您不需要直接修改基础字段,请使用auto-property:
static string stringToSet {get; set;}
Run Code Online (Sandbox Code Playgroud)