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
Superpowers for your AI  29 Mar 2026
Autotitling Windows Terminal Tabs  17 Mar 2026
Don't assume shared understanding  25 Jan 2026
Better Table Tests in Go  21 Oct 2025
Error assertions  26 Apr 2025
Browsers and WSL  31 Mar 2024
Factory methods and functions  05 Mar 2023
Using Constructors  27 Feb 2023
An Inconvenient API  18 Feb 2023
Method Archetypes  11 Sep 2022
Archives
April 2008
2008