Now here’s a trick that I didn’t know - you can use the [Conditional] attribute to suppress a method unless a given symbol is defined at compile time.

From the MSDN documentation:

Applying ConditionalAttribute to a method indicates … the method should not be compiled into Microsoft intermediate language (MSIL) unless the conditional compilation symbol that is associated with ConditionalAttribute is defined.

Mostly of use when you’re writing logging and other diagnostic code - indeed, major use in the framework seems to be in the Debug and Trace classes.

See details on MSDN at http://msdn2.microsoft.com/en-us/library/system.diagnostics.conditionalattribute. aspx

Comments

blog comments powered by Disqus
Next Post
The End of an Era  10 Apr 2008
Prior Post
Renaming WPF Windows  07 Apr 2008
Related Posts
Error Methods  25 Nov 2017
Pass implementations, not representations  14 Oct 2017
Avoiding the Singleton Pattern  22 Jul 2017
Implementing the Singleton Pattern  15 Jul 2017
Static Analysis tools for the Win  15 Apr 2017
Exception Logging  19 Jan 2016
On the Merits of Simple Code  28 Nov 2015
Semantic Types  27 Sep 2015
Command Line Processing  21 Sep 2014
Easy String Conversion  24 Aug 2014
More smart-code posts »
Related Pages
April 2008 archive