Monday, January 21, 2008

Stupid Preprocessor Tricks in C#

Did you know that this compiles:

#region methods

private

#endregion

void SomeMethod()
{
}



Neat, huh? You can just jam #region and #endregion directives everywhere, to make your code SUPER DUPER UNREADABLE. How about right in the middle of a statement? NO PROBLEM.

#region INSANITY
private void SomeMethod()
{
string x = "I like " +

#endregion INSANTIY

"bees";
}


Neat, huh? So, my question is simple... Is there any reason you'd want to put region directives right in the middle of a routine, let alone a statement?

Edit: it makes sense that it would be so, since region directives are essentially whitespace to the compiler. I just found it amusing (as in I screwed around for a half hour trying to figure out why I was getting a 'more than one protection modifier' error, a collapsed region was hiding an extra "public" keyword.)

No comments: