Returning value of static property from public instance property
- by Jamie Dixon
I was just playing around with some code in LINQPad and managed to crash the program with a stackoverflow exception.
I basically created a static property in a class and used another property to return the value from an instance.
The getter of my instance property would return the value of the static property, but the setter would set itself. When would this type of pattern be used and how come it generated a stackoverflow exception?
Code example of what I did:
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; }
}
}