In C++ we can use #ifdef to eliminate some debug statements when we release. C# is different from C++ in preprocessor. Can I still get the same result useing C# #if. We want to
You can wrap code in:
#if DEBUG
// debug only code
#endif
However, I don't recommend this. It's often a better alternative to just make a method and flag it with the [Conditional("DEBUG")]
attribute. See Conditional on MSDN for more details. This allows you to make debug only methods:
[Conditional("DEBUG")]
public void DebugPrint(string output) { // ...
}
Then, you can call this normally:
DebugPrint("Some message"); // This will be completely eliminated in release mode
Use something like:
#if DEBUG
System.Console.WriteLine("This is debug line");
#endif
The according the MSDN docs
The scope of a symbol created by using #define is the file in which it was defined.
So you can't have a file that defines several other defines that are used throughout your program. The easiest way to do this would be to have different configurations on your project file and specifying the list of defines for each configuration on the command line to the compiler.
Update: You can set your project defines in Visual Studio by right-clicking on your project and selecting Properties. Then select the Build tab. Under general you can specify the defines to be sent to the compiler under "Conditional compilation symbols". You can define different project settings using the Configuration Manager (Build->Configuration Manager)
Update 2:
When the "Conditional compilation symbols" are specified, Visual Studio emits a /define
on the command line for the compiler (csc.exe for C#), you can see this by examining the output window when building your project. From the MSDN docs for csc.exe
The /define option has the same effect as using a #define preprocessor directive except that the compiler option is in effect for all files in the project. A symbol remains defined in a source file until an #undef directive in the source file removes the definition. When you use the /define option, an #undef directive in one file has no effect on other source code files in the project.
You can use symbols created by this option with #if, #else, #elif, and #endif to compile source files conditionally.