Pregunta

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?

¿Fue útil?

Solución

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#.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top