StackOverflowException

I was developing today and got a StackOverflowException, and I quickly realized what it was.  One of the ways you get this is if you do "too much" in the constructor; that is, you can do limited things in the constructor.  If you get this from something you are doing in the constructor, that means you can't do it.

I also get this if you get a circular reference, meaning that you have a property like this:

public string Text
{
   get { return this.Text; }
}

This is a recursive, infinite loop when accessed, when you really meant to do this:

public string Text
{
    get { return _text; }
}

So watch out for those things, and know that this exception may mean you have an infinite loop in your code.

Comments

# re: StackOverflowException

Wednesday, February 13, 2008 2:18 PM by alex

thanks for that!!  I've been pondering a stack overflow exception for the last couple of hours and did not notice the missing '_' in my code.

Anything else I found talked about infinite loops which i knew was not the issue.