ALERT

[FREE DEMO] Deploy Your Enterprise Cloud in Minutes

Conditional Attribute

Definition - What does Conditional Attribute mean?

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

A conditional attribute indicates a condition to specify conditional compilation wherein methods are selectively called on the basis of definition of symbols. It instructs the compiler to compile (into Microsoft Intermediate Language) or not compile specific blocks of code, based on a condition - whether or not a specific conditional compilation symbol is defined. If specific symbols have not been defined at the instant they are called, then calls to that method or class will be ignored by the compiler.

Techopedia explains Conditional Attribute

A conditional attribute has the following characteristics:

  • It can be applied to methods and classes, but only if they are derived from an attribute.
  • While applying conditional attribute to a class, the attribute class will be emitted to metadata only if the conditional compilation symbol is defined.
  • It takes one parameter, which is the identifier symbol controlling the compilation.
  • The arguments passed to a conditional method or attribute class will be type-checked by the compiler.
  • It is completely taken care of by the compiler and not by the run time.
  • It cannot be applied to a method used in a delegate-creation expression.
  • It does not affect the code generated for conditional method, but does affect the call to the method.

A conditional attribute provides a declarative programming pattern and helps to easily maintain source code. By applying a conditional attribute at the method level, source code is more readable. The caller of a method need not take additional code for conditional compilation. A conditional attribute is used to enable tracing and logging functionalities in the debug builds by using the identifier DEBUG to display and log diagnostic information related to the application. Also, it helps to separate logic related to the debug build (in systems used for development) from release builds, which are deployed in sites and applications. To maintain multiple editions (with selected features in some edition) of a software without code duplication, conditional compilation forms the right technique by which the single main source code is maintained for multiple editions based on the respective symbols defined for them.

Following are few rules to follow while using a conditional attribute:

  • A conditional method inside a class or struct declaration must have a return type as void.
  • While using multiple identifiers as conditional attributes, the inclusion of method(s) is based on the result of logical OR or logical AND on the defined symbols.
  • It cannot be specified on a method in an interface declaration.
  • A conditional method cannot be preceded with "override" keyword, but it can be virtual. If overridden, it is considered to be a conditional implicitly.
  • Symbols considered for conditional compilation can be defined as compiler command-line options or as environment variables from the OS shell or as pragmas (using preprocessor directive "#define") in the source code.
  • Unlike C++, definition of symbols in C# can be in any order and hence the ordering between "#define" and a conditional method has to be properly set.

Although usage of "#if and #endif" forms an alternate option to conditional attribute, the latter provides a more clean, elegant and less error-prone approach when compared to the former. Internally, the difference is that while using a conditional attribute for a method, method will still be a part of the assembly and not loaded. But in the case of #if / #endif, method will not be visible in the assembly itself.

This definition was written in the context of C#

Techopedia Deals

Connect with us

Techopedia on Linkedin
Techopedia on Linkedin
Tweat cdn.techopedia.com
"Techopedia" on Twitter


'@Techopedia'
Sign up for Techopedia's Free Newsletter!

Email Newsletter

Join thousands of others with our weekly newsletter

Resources
The 4th Era of IT Infrastructure: Superconverged Systems
The 4th Era of IT Infrastructure: Superconverged Systems:
Learn the benefits and limitations of the 3 generations of IT infrastructure – siloed, converged and hyperconverged – and discover how the 4th...
Approaches and Benefits of Network Virtualization
Approaches and Benefits of Network Virtualization:
Businesses today aspire to achieve a software-defined datacenter (SDDC) to enhance business agility and reduce operational complexity. However, the...
Free E-Book: Public Cloud Guide
Free E-Book: Public Cloud Guide:
This white paper is for leaders of Operations, Engineering, or Infrastructure teams who are creating or executing an IT roadmap.
Free Tool: Virtual Health Monitor
Free Tool: Virtual Health Monitor:
Virtual Health Monitor is a free virtualization monitoring and reporting tool for VMware, Hyper-V, RHEV, and XenServer environments.
Free 30 Day Trial – Turbonomic
Free 30 Day Trial – Turbonomic:
Turbonomic delivers an autonomic platform where virtual and cloud environments self-manage in real-time to assure application performance.