Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running different code in debug and release settings visual studio

Is it possible in C# to do run specific lines codes in debug setting and other in say release settings.

if #debug

//run some lines of code

else 

// run different lines of code
like image 402
Fahim A. Salim Avatar asked Jun 22 '12 10:06

Fahim A. Salim


People also ask

How do I run multiple debugger codes in Visual Studio?

To debug multiple services at the same timeOpen the folder corresponding to your first service in VS Code. In VS Code, select File > Add Folder to Workspace…, and pick the folder corresponding to your other service.

How do I change the run and Debug code in Visual Studio?

To bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D. The Run and Debug view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.

What is the difference between release and Debug in Visual Studio?

By default, Debug includes debug information in the compiled files (allowing easy debugging) while Release usually has optimizations enabled. As far as conditional compilation goes, they each define different symbols that can be checked in your program, but they are language-specific macros.


2 Answers

You can do something like:

#if DEBUG
// Debug Code

#else
// Release Code

#endif

I use that in WCF services to run it as a console app in debug, but as a Windows Service in release

HTH, Rupert.

like image 104
Rupert Avatar answered Sep 28 '22 17:09

Rupert


Read this blog post If You’re Using “#if DEBUG”, You’re Doing it Wrong, the author suggests using System.Diagnostics.ConditionalAttribute:

[Conditional("DEBUG")]
private static void DebugMethod()
{
    // Debugging code
}
like image 20
JohnTube Avatar answered Sep 28 '22 18:09

JohnTube