views:

879

answers:

4

Referring to the "Configuration Manager" under the Build menu.

Is there a way to comment my C# code so that the commented out code does not compile while the solution is in Debug mode, but would compile if I changed it to Release mode?

Update: Judging by the answers, I should have said why I wanted this. The reason I want to have code that will be compiled in Release mode but not in Debug is that I've got some code that will not work from my dev PC (code that sends emails from my host, etc...).

Instead of having to run back through my code and uncomment lines before publishing, I'd like that to be automatic.

+6  A: 

Are you looking for something like this?

#if DEBUG
     Console.WriteLine("Debug Mode");
#else
     Console.WriteLine("Release Mode");
#endif

If you only care about release mode, you can use:

#if !DEBUG
     Console.WriteLine("Release Mode");
#endif
BlueMonkMN
I would say the same issue applies to this solution as to mine. I wouldn't recommend it. I would suggest looking at the problem as a whole and seeing if there is some other way to solve the bigger issue. Having lots of #if statements or Conditional attributes littering your code is a sure way to create lots of confusion for any maintenance programmer (or even yourself in a couple of months time!)
Colin Mackay
Why do you assume that the DEBUG symbol is being used wrongly? It is added by default to every project and the framework is there to use it, so certainly it's intended to be used. I do find it a bit unusual that you would only compile some code in release mode and not debug mode (usually it's the other way around), but compiling different code between release mode and debug mode is an intentional and useful feature of the environment.
BlueMonkMN
Nothing wrong with an occasional conditional compilation flag. I agree when you see more than a handful it's confusing, though
James D
This is it... thank you! Just wanted to be able to hide some code while in DEBUG mode versus Release mode. Works great!
Chad
I understand the reservations. In this case, there will only be a couple uses for me. Just a few lines of code that try to send emails from my host - which doesn't work from my dev box. Just don't want to have to run back through my code to uncomment before release. Thanks!
Chad
@BlueMonkMN: I would suggest that any code that works differently between debug and release builds has potential issues with it. It makes testing difficult and problematic. You have to be very careful that any addional code in one build over the other has no unintentional side effects - and it is quite easy to introduce them if found.
Colin Mackay
Potential, yes. Be careful, yes. But conditional compilation is useful and often exactly the right solution to use.
Robert Rossney
+8  A: 

You could use the Conditional attribute on methods (but not individual lines of code) for this purpose

e.g. The following will only be compiled into DEBUG builds.

[Conditional("DEBUG")]
public void MyMethod()
{
    // Do Stuff
}

The DEBUG symbol is already specified in the project settings. You'd have to create your own symbol for a release build, say "RELEASE", so that you can do this:

[Conditional("RELEASE")]
public void MyMethod()
{
    // Do Stuff
}

However, I'd recommend stepping back and looking at your problem anew from a higher level as I wouldn't really recommend this solution.

Colin Mackay
Not a direct solution to what I wanted, but good to know. Thank you.
Chad
A: 

I may be wrong, but I think that comments are ignored by the compiler. If I look at an assembly of mine using .net reflector, I don't see any comments that I know exist.

BlueMonkMN's method will work to run different code depending on compile mode.

If you want to have different code running depending on what mode of compile (and other variables) you are using, check out PostSharp. It's a post-compiling compiler that can add and remove code to your assembly.

Example use: - I love to have detailed debug and trace info for my projects. - I hate having a print or trace.write statement after every method result or method call, as this extra debugging code obscures the function doing the work.

You can configure PostSharp to create this extra debug info dynamically! A couple config tweaks and you can have every call to every function printed AND the result (with variable contents) from each call. This makes it very easy to follow the program logic flow.

Lanceomagnifico
Reread the question. He wanted conditional compilation but didn't know it existed.
John Saunders
A: 

I would attempt to solve your problem with an object oriented technique. Using dependency injection, I would construct a class that performs your necessary Debug actions.

Something like:

public class MyClass {

  public MyClass(IDoOtherStuff stuffToDo) {

     DoOtherStuff = stuffToDo;

  }

  private IDoOtherStuff DoOtherStuff { get; set; }

  public void Do() {

    DoOtherStuff.BeforeDo();

    // Blah blah blah..  

    DoOtherStuff.AfterDo();    

  }

}

public interface IDoOtherStuff {
   void BeforeDo();
   void AfterDo();
}

public class DebugOtherStuff : IDoOtherStuff {

   public void BeforeDo() {
      Debug.WriteLine("At the beginning of Do");
   }

   public void AfterDo() {
      Debug.WriteLine("At the end of Do");
   }
}

public class ReleaseOtherStuff : IDoOtherStuff {
   public void BeforeDo() { }
   public void AfterDo() { }
}

Now, you can use an IoC container like Windsor, Unity, Ninject, or Spring.Net to configure your development environment versus Release environment.

Jeff Fritz