Causing a stackoverflowexception in the webserver(!)

The other day I was working on some coding at work and after adding quite an ordinary property (as follows) the browser went totally berserk, displaying some error message about "service unavailable" (in red, I might add ;-) and a while later the visual studio debugging prompt let me know that there was an stackoverflowexception in aspnet_wp.exe. That kind of behavior obviously sets one off, and after some misleading googling I finally spotted my fatal error, can you see it?:

private string myProperty;
public string _myProperty 
    get { return _myProperty; }
    set { myProperty = value; }

Well, what I accidentally did, as you can see above in the get block i return the property itself instead of the private member variable, causing an infinite loop... not good.

By the way, I prefer the following namingconvention when it comes to underscores and capitals, but thats not what goes at work. In the following example (with the same error!) I think the error is easier to spot:

private string _myProperty;
public string MyProperty 
    get { return MyProperty; } //Still wrong! Should be: return _myProperty;
    set { _myProperty = value; }

There are other reasons to the same error behavior, so google it if this isn't yours.

No comments :

Post a Comment