Conditional Attribute in C# compared to #if #endif
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   .NET Framework Fundementals
Current Lesson:
Conditional Attribute in C# compared to #if #endif
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Database Synchronization the easy way [next Lesson]  ASP.NET 2.0 – What’s New?
Conditional Attribute in C# compared to #if #endif
  by: webmaster

So its pretty much common practise for many to use #if and #endif directives to create different builds in your application while using the same source. In a previous tutorial I displayed how you can use the #if directive to force your methods to not ASP.NET caching during debug mode (ASP.NET Caching Techniques).

The #if #endif directives can be useful at times can make source code hard to read. As an alternative one could use the C# conditional attribute.

The C# conditional attribute basically allows you to control when a method should and should not be called. The benefit of the C# conditional attribute is that it is applied at the method level, which results in source code that is more readable.

To show you an example of the conditional attribute, let's use it to output debugging statements. We're going to create a method that displays debugging information and then were going to add this method to a classes. Note that this debugging method will only be a part or classes when they have set a particular environmental variable.

So our debugging method that will look something like this:

[ Conditional("Debug") ]
public void DebugThis()
{
   // get the name of the calling routine using Reflection
   //
   string _callingMethod = new StackTrace().GetFrame(1).GetMethod().Name;

   Trace.WriteLine("Entering DebugThis called by:" + _callingMethod);




// insert other checks here like Asserts etc. // Trace.WriteLine("Exiting DebugThis for " + _callingMethod); }
So the method above marked with the conditional attribute will be called in all of our properties.
public string UserName
{
 get
 {
  DebugThis();
  return _userName;
 }
}
So, the method DebugThis() has been marked to be used only during debug mode. The conditional attribute tells the compiler that when the debug environmental variable is used, and then and only then do we call the DebugThis method. So if the DEBUG environmental airboat is not defined, the property UserName will look like:
public string UserName
{
 get
 {
  return _userName;
 }
}
Please note that regardless of the debug environment variable i.e. if it defined or not, the DEBUG this method will be compiled into your assembly. But keep in mind that when the debug environmental airboat is not defined, the debug method will be in the assembly. But it does not get loaded into memory, nor is it JITed unless it is called.

You can also use other predefined symbols like Conditional("Trace") or even combine more than one like:

[Conditional("DEBUG"), Conditional("TRACE")]
Or even create your own symbols and use them.

So in summary, the Conditional Attribute enables developers to better separate conditional code when compared to the #if directives and it also how to structure your code more cleanly.


1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  .NET Framework Fundementals
Current Lesson:
Conditional Attribute in C# compared to #if #endif
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Database Synchronization the easy way [next Lesson]  ASP.NET 2.0 – What’s New?


Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
Al_Pennywo.. 2
C#fanatic 2
simboy 1

Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center