我刚刚在LINQPad中玩弄了一些代码,并设法使程序崩溃并出现一个stackoverflow异常。从公共实例属性返回静态属性的值
我基本上在字段中创建了一个静态属性,并使用一个属性来返回实例的值。
我的实例属性的getter会返回静态字段的值,但setter会自己设置。什么时候会使用这种类型的模式,以及如何产生一个stackoverflow异常?我所做的
代码示例:
void Main()
{
SomeClass myinstance = new SomeClass();
SomeClass.x = "Some Value";
myinstance.y = "Some other value";
myinstance.y.Dump();
}
public class SomeClass
{
public static string x;
public string y
{
get { return x; }
set { y = value; }
}
}
哦,是的!谢谢马克。什么菜鸟的错误:P – 2010-04-18 19:08:38
容易让人......我敢肯定很多人都这样做:) – 2010-04-18 19:10:02