views:

107

answers:

1

Hi,

I had the following code:

Contract.Requires(somecondition, "some message"/*some comment*/);

and while debugging at some moment the condition wasn't met, and it threw the exception with the usual text, plus the comment! :P So I got this exception:

ContractException was unhandled.

Precondition failed: somecondition. "some message"/*some comment*/

So my question is, is this a bug or what? The second parameter of the method is of type string, but it only accepts literals, so I know the entire method is a hack, but it shouldn't accept that comment, right?

+1  A: 

This is because the "Code Contracts rewriter" rewrites the statement to enable the user to see all the information in the resulting exception message (including the condition expression itself, and apparently comments too).

http://visualstudiomagazine.com/articles/2010/06/23/code-contracts.aspx

ulrichb
Well yeah, but shouldn't all programs function exactly the same if all of its comments were removed? And btw, the guy you refer to says the code is reordered áfter compilation to IL, and since IL doesn't contain the comments from C#, howcome the comment pops up in the exception ?
JBSnorro
The "rewiter" *cannot* use just the IL code, because the IL code does *not* contain the variable names, ... so it definitely uses the source code files.
ulrichb