- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are conditional attributes in C#?
Attributes are used for adding metadata, such as compiler instruction and other information such as comments, description, methods, and classes to a program.
This predefined attribute marks a conditional method whose execution depends on a specified preprocessing identifier.
It causes conditional compilation of method calls, depending on the specified value such as Debug or Trace. For example, it displays the values of the variables while debugging a code.
The following is the syntax of conditional attributes −
[Conditional( conditionalSymbol )]
Let us see how to work with Conditional attributes −
Example
#define DEBUG using System; using System.Diagnostics; public class Myclass { [Conditional("DEBUG")] public static void Message(string msg) { Console.WriteLine(msg); } } class Test { static void function1() { Myclass.Message("In Function 1"); function2(); } static void function2() { Myclass.Message("In Function 2"); } public static void Main() { Myclass.Message("In Main function"); function1(); Console.ReadKey(); } }
Output
In Main function In Function 1 In Function 2
Advertisements