The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Conditional Methods

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Eric Gunnerson

Posts: 1006
Nickname: ericgu
Registered: Aug, 2003

Eric Gunnerson is a program manager on the Visual C# team
Conditional Methods Posted: Aug 13, 2004 8:46 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Eric Gunnerson.
Original Post: Conditional Methods
Feed Title: Eric Gunnerson's C# Compendium
Feed URL: /msdnerror.htm?aspxerrorpath=/ericgu/Rss.aspx
Feed Description: Eric comments on C#, programming and dotnet in general, and the aerodynamic characteristics of the red-nosed flying squirrel of the Lesser Antilles
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Eric Gunnerson
Latest Posts From Eric Gunnerson's C# Compendium

Advertisement

I saw an internal post today about somebody who wanted to get rid of their #if DEBUG statements in their code, because they were ugly. That made me realize that there's a feature that not everybody knows about, known as conditional methods.

Consider the following code:

using System; using System.Diagnostics; 
class Program { static void Main(string[] args) {
#if DEBUG DebugMethod1(); #endif
DebugMethod2(); Console.ReadKey(); } static void DebugMethod1() { Console.WriteLine("Debug1"); } [Conditional("DEBUG")] static void DebugMethod2() { Console.WriteLine("Debug2"); } }

In my call to DebugMethod1(), I've used the traditional #if approach to only compile the call if the DEBUG symbol is defined. But in the call to DebugMethod2(), I've used a conditional method that has the same effect - if the DEBUG symbol is not defined, it's as if the call isn't there. That includes any side effects from parameter evaluation - they don't happen.

Note that the docs on the ConditionalAttribute class are a bit misleading.

Read: Conditional Methods

Topic: A First Look at SQL Server 2005 for Developers Previous Topic   Next Topic Topic: Classes at OIT - C# and Applied Web Services

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use