Conditional attribute

What is a conditional attribute?
A conditional attribute is a tag used to mark a method or class the execution of which depends on the definition of the preprocessing identifier.

A condition attribute specifies a condition to indicate conditional compilation, where methods are selectively called based on the definition of symbols. It instructs the compiler to compile (based on a condition) (in Microsoft Intermediate Language) or not to compile certain blocks of code - regardless of whether a certain conditional compilation symbol is defined or not. If certain symbols were not defined at the time of the call, calls to this method or class are ignored by the compiler.

A conditional attribute has the following properties:

It can be applied to methods and classes, but only if they are derived from an attribute.
When a conditional attribute is applied to a class, the attribute class is sent to metadata only if the conditional compilation symbol is set.
It takes a parameter that represents the compilation symbol.
The arguments passed to a conditional method or attribute class are typed by the compiler.
It is completely done by the compiler, not the runtime.
It cannot be applied to a method that is used in a delegate creation expression.
It does not affect the code generated for the conditional method, but it does affect how the method is called.
A conditional attribute provides a declarative programming pattern and helps to maintain the source code easily. The use of a conditional attribute at the method level makes the source code easier to read. The caller of a method does not need any additional code for the conditional compilation. A condition attribute is used to enable tracing and logging functionality in the debug builds by using the DEBUG identifier to view and log diagnostic information related to the application. It also helps to separate the logic related to the debug build (in systems used for development) from the release builds that are deployed in sites and applications. To manage several editions (with selected functions in one edition) of a software without code duplication,

It consequences some rules to keep in mind when using a conditional attribute:

A conditional method in a class or structure declaration must have the return type void.
When using multiple identifiers as conditional attributes, the inclusion of the method (s) is based on the result of logical OR or logical AND on the defined symbols.
It cannot be specified for a method in an interface declaration.
A conditional method cannot be preceded by the keyword 'override', but it can be virtual. If it is overwritten, it is implicitly considered conditional.
Symbols that are taken into account for the conditional compilation can be used as compilerCommand line options or as environment variables from the operating system shell or as pragmas (using the preprocessor directive '#define') in the source code.
In contrast to C ++, the definition of symbols in C # can be done in any order and therefore the order between '#define' and a conditional method must be correctly defined.
Although using '#if and #endif' is an alternative option for the conditional attribute, the latter offers a cleaner, more elegant, and less error-prone approach compared to the former. Internally, the difference is that when you use a conditional attribute on a method, the method is still part of the assembly and is not loaded. In the case of #if / #endif, the method is not visible in the assembly itself.

Was the explanation to "Conditional attribute"Helpful? Rate now:

Weitere Erklärungen zu Anfangsbuchstabe C