1

I just read http://msdn.microsoft.com/en-us/library/system.diagnostics.conditionalattribute.aspx.

And I'm struggling to understand what the difference is between using

[Conditional("CONDITION1")] 

with/without

#define CONDITION1

and just commenting/uncommenting a method?

Is the only purpose of ConditionalAttribute to make the compiler ignore a method or attribute? If so, why not juse use a comment? If not, what are the advantages compared to just using comments?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
David Klempfner
  • 8,700
  • 20
  • 73
  • 153
  • 1
    You can easily define symbols at build time e.g. `csc /define:CONDITION1`. It's much harder to comment out entire method bodies. – Lee Jan 06 '14 at 23:33
  • 1
    Seems to me you will be able to re-use it for multiple methods. That way you won't have half your code in comments but you just have that one `#define CONDITION1`. – Jeroen Vannevel Jan 06 '14 at 23:33

1 Answers1

3

With conditional attributes you can enable and disable multiple parts of code with a single step, rather than having to hunt through your source code and comment/uncomment multiple locations.

Conditional attributes can also provide information as to why certain code is enabled or disabled (e.g. for debugging purposes etc.)

Also, you should prefer conditional attributes to conditional compilation (#if etc.), as explained in Effective C#.

Ergwun
  • 12,579
  • 7
  • 56
  • 83