The compiler does not allow this, because the semantics of code like this will be undefined or, at best, quite difficult to understand:
[System.Diagnostics.Conditional("DEBUG")] public int foo() { ... } var x = someOtherMethod(foo());
The [Conditional("DEBUG")] attribute for a method means that method calls are excluded from compiled code if the "DEBUG" character is present.
But if the call to foo() disappears from the compiled code, what is passed to someOtherMethod() ? Or, if this call is also deleted, what to assign x ? How to guarantee that local x even has a value that usually causes a compilation error?
The .NET team decided not to go this way, instead they added a compilation time limit, which the [Conditional()] methods should be invalid.
source share