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 −
#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(); } }
In Main function In Function 1 In Function 2