SIGN UP MEMBER LOGIN:    
ARTICLE

Conditional Methods in C#

Posted by Javeed M Shaikh Articles | .NET 4.5 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.
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Nevron Gauge for SharePoint
Become a Sponsor