Information Technology Reference
In-Depth Information
Compilation symbol
↓
[Conditional( "DoTrace" )]
static void TraceMessage(string str)
{
Console.WriteLine(str);
}
Example of Conditional Attribute
The following code shows a full example using of the
Conditional
attribute.
Method
Main
contains two calls to method
TraceMessage
.
The declaration for method
TraceMessage
is adorned with the
Conditional
attribute,
which has the compilation symbol
DoTrace
as its parameter. So if
DoTrace
is defined, the
compiler will include the code for all the calls to
TraceMessage
.
Since the first line of code defines a compilation symbol named
DoTrace
, the compiler
will include the code for both calls to
TraceMessage
.
#define DoTrace
using System;
using System.Diagnostics;
namespace AttributesConditional
{
class Program
{
[Conditional( "DoTrace" )]
static void TraceMessage(string str)
{
Console.WriteLine(str);
}
static void Main(string[] args)
{
TraceMessage("Start of Main");
Console.WriteLine("Doing work in Main.");
TraceMessage("End of Main");
}
}
}