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
Prefer declarative method names  03 Feb 2018
Using Premeditation  27 Jan 2018
With Relocation  20 Jan 2018
Using Consolidation  13 Jan 2018
Using Semantic Types  06 Jan 2018
Using Extension Methods  30 Dec 2017
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
More smart-code posts »
Related Pages
April 2008 archive