I was just reading the blog entry corner cases in C# and Java when I remember two of such corner cases I hit

Multi-line comments

I was working on a tool to count source lines. In that I printed out number of uncommented lines as well. While working on it I commented out a block of code (I hate commented lines of code in production sources [:)] ) and was surprised at the result

/*

string startDelin = "/*"

string endDelim = "*/";

string oneLine = "//";

...

*/

Its apparent from the source coloring that the */ inside the string endDelim was considered as the comment block end-delimiter. I thought it was a bug in the begining, however a look into the lexical grammer in the C# 1.2 spec Section 2.3.2 clarified that this is indeed the expected behavior.....

Pre-processor directive

This one is real corner case. I don't remember where I first saw this, but it goes as follows. There was a block of conditional code as in

#if FOO

/*

#else

/* some comment */

class MyClass { }

#endif

The /* was lying around in a big block of code that was never used as FOO was never defined. When the same source got compiled with FOO defined,  still in the code the class MyClass could be found even though it's inside the #else block. The reason is as follows

#define FOO

 

namespace WeirdPreProcessing

{

#if FOO

/*

#else

/* some comment */

class MyClass { }

#endif

So defining FOO made the #else commented out and hence everything inside the #else block became a part of the #if block!!!