Returning value of static property from public instance property

Posted by Jamie Dixon on Stack Overflow See other posts from Stack Overflow or by Jamie Dixon
Published on 2010-04-18T19:02:20Z Indexed on 2010/04/18 19:13 UTC
Read the original article Hit count: 523

Filed under:
|

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; }
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about linqpad