Reader Level:
Articles

Conditional Methods in C#

By Javeed M Shaikh on October 25, 2011
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.
  • 0
  • 0
  • 5096

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.

namespace BusinessFunctions
{
   public class LoadData
   {
       [Conditional("DEBUG")] // this method is only called in debug mode.
       public static void TestLoadData(string traceMessage)
       {
           Console.WriteLine("[TRACE] - " + traceMessage);
       }
   }
}

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.

namespace MyConsoleApp
class Program
    {
static void Main(string[] args)
        {
            BusinessFunctions.LoadData.TestLoadData("Main Starting");
  
      if (args.Length == 0)
      {
          Console.WriteLine("No arguments have been passed");
      }
      else
      {
          for( int i=0; i < args.Length; i++)   
          {
              Console.WriteLine("Arg[{0}] is [{1}]",i,args[i]);
          }
      }
 
      BusinessFunctions.LoadData.TestLoadData("Main Ending");
       Console.ReadKey();
}
}

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.

COMMENT USING

Trending up