Welcome to MSDN Blogs Sign in | Join | Help
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.

Posted: Thursday, September 23, 2004 2:46 PM by sebby1234
Filed under:

Comments

matt said:

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.
# September 23, 2004 3:01 PM

David M. Kean said:

I think C# actually outputs a warning when you do this in Visual Studio 2005.
# September 23, 2004 3:34 PM

Sebastien St-Laurent said:

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 :)
# September 23, 2004 3:39 PM

no warning in vs2003 said:

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.
# September 23, 2004 4:22 PM

James said:

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.
# September 23, 2004 5:13 PM

dz said:

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...
# September 23, 2004 9:10 PM

Di .NET e di altre amenita' said:

# September 24, 2004 8:49 AM
Anonymous comments are disabled
Page view tracker