Conditional Methods in C#

This article will show how to use conditional methods, which provide a powerful mechanism by which calls to methods can be included or omitted depending on whether a preprocessor symbol is defined.
 
It happens so many times when are developing or debugging an application and you want that your application should behave differently when you deploying in "Debug" mode and differently when you are deploying in "Release" mode. This is where the Conditional attribute in .NET comes to the rescue.
 
So let's say for example you have a method which takes some arguments from the command line and you need to test this you need to restart this application many times under different scenario's while debugging. But since it's not yet ready for release you want to test under different parameter conditions and then ignore them when you are deploying. There may be many other situations when this can be used and what I have represented here is a very simple situation.
 
In the following example I have created a LoadData class which has a static method TestLoadData, your implementation can differ but you will get an idea on how it is being used here. Please note the conditional attribute, [Conditional("DEBUG")] so that the method is only called when we use "Debug" mode from the configuration list.
  1. namespace BusinessFunctions  
  2. {  
  3.    public class LoadData  
  4.    {  
  5.        [Conditional("DEBUG")] // this method is only called in debug mode.  
  6.        public static void TestLoadData(string traceMessage)  
  7.        {  
  8.            Console.WriteLine("[TRACE] - " + traceMessage);  
  9.        }  
  10.    }  
In the example below, when using the 'Debug' configuration, the method will execute. When using 'Release' configuration, this method will not be called. Since the "TestLoadData" method has been decorated with the conditional attribute, the compiler removes references to it. For this reason, methods decorated with the conditional attribute must be 'void' return type.
  1. namespace MyConsoleApp  
  2. class Program {  
  3.   static void Main(string[] args) {  
  4.     BusinessFunctions.LoadData.TestLoadData("Main Starting");  
  5.   
  6.     if (args.Length == 0) {  
  7.       Console.WriteLine("No arguments have been passed");  
  8.     } else {  
  9.       for (int i = 0; i < args.Length; i++) {  
  10.         Console.WriteLine("Arg[{0}] is [{1}]", i, args[i]);  
  11.       }  
  12.     }  
  13.   
  14.     BusinessFunctions.LoadData.TestLoadData("Main Ending");  
  15.     Console.ReadKey();  
  16.   }  
When you run this application with "Debug" option you will get the output as below
 
[TRACE] - Main Starting
No arguments have been passed
[TRACE] - Main Ending
 
When you run this application with "Release" option you will get the output as below:
 
No arguments have been passed
 
Using the conditional attribute makes it easy to debug and deploy without changing your code for each environment.
 
I hope this article will help you debug you application easily in different environments.