Hello, today I will show you how to use ConditionalAttribute from .NET.
Let’s say you want execute some method in when DEBUG compilation symbol is defined (i.e. in development only). Here is a snipped of code which allows you to achieve this:
class Program { static void Main(string[] args) { TraceData(); DoSomeWork(); } [Conditional("DEBUG")] private static void TraceData() { Console.WriteLine("Some debugging info"); } private static void DoSomeWork() { Console.WriteLine("Some production code.."); } }
In addition, here is “Build” tab from “Project properties” window:
If you uncheck “Define DEBUG constant” and execute app, you will get the following output:
As you can see, TraceData method didn’t execute. Hope you will find places to use this attribute in your own projects.