C# Member Properties Recursion?!?!?


I was working on some C# tool today when i came along this piece of code i just wrote.

public IKJointConstraint JointConstraint

{

 get { return JointConstraint; }

 set { JointConstraint = value;}

}

When i was actually trying to use m_JointConstraint not JointConstraint. What i found interesting is that this piece of could would lead to an infinite recusion loop and considering that the function does nothing in particular, I was surprised the compiler did not find this.

Comments (8)

  1. matt says:

    Wow, cool. I guess you could actually make a recursive accessor actually DO something… that would be even cooler.

    sidenote: I’m kinda disappointed at how recursion is one of those super-elegant ideas, but in practice in most cases just steals memory and cpu-cycles. Sigh.

    nice find.

  2. I think C# actually outputs a warning when you do this in Visual Studio 2005.

  3. Ahh I see. I am actually using a special build system but i think it uses the VS 2003 compiler under the hood.

    Having written compilers/interpreters in the past, I was wondering why they wren’t trapping this as it is something trivial to detect.

    Guess they are already on top of that one for the next release 🙂

  4. I’ve been bit by this several times – there’s no warning in vs2003 for infinite recursion in the property getter / setter. So what ends up happening is a stack overflow at runtime.

  5. James says:

    Heh, I got stung by that a couple of days ago. I spent 10 minutes look at the code not understanding why I was getting a stackoverflow. The second after I showed it to a co-worker we both saw it simultaneously. Go figure.

  6. dz says:

    I did the same thing a couple days ago, but stepping through it and watching the cursor bounce in and out of the method a couple times did the trick. Glad to hear this has been fixed…