C # how can I use #if to get another compilation result for debugging and release?

In C ++, we can use #ifdef to eliminate some debugging commands on release. C # is different from C ++ in the preprocessor. Can I get the same result using C # #if. We want to exclude all debug statements by changing one place, and we have several different types of debug statements. Can a single file containing ALL of our #ifdef flags enable or disable these debug statements? thank

+5
source share
3 answers

According to MSDN Docs

The volume of a character created with #define is the file in which it was defined.

, , , . - .

Update: Visual Studio, . . , " ". Configuration Manager (Build- > Configuration Manager)

2: " ", Visual Studio /define (csc.exe #), , . MSDN csc.exe

/define #define , . #undef . /define, #undef .

, #if, #else, #elif #endif .

+2

:

#if DEBUG

// debug only code

#endif

. [Conditional("DEBUG")]. . " MSDN" . :

[Conditional("DEBUG")]
public void DebugPrint(string output) { // ... 
}

:

DebugPrint("Some message"); // This will be completely eliminated in release mode
+20

Use something like:

#if DEBUG

System.Console.WriteLine("This is debug line");

#endif
+4
source

All Articles