Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# how can I use #if to have different compile result for debug and release?

Tags:

c#

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 eliminate ALL debug statements by change one place and we have several different types of debug statements. Can have one file which contains ALL our #ifdef flags to turn on or turn off those debug statements? thanks

like image 771
5YrsLaterDBA Avatar asked Feb 09 '10 19:02

5YrsLaterDBA


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


3 Answers

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
like image 148
Reed Copsey Avatar answered Oct 05 '22 20:10

Reed Copsey


Use something like:

#if DEBUG

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

#endif
like image 20
hongliang Avatar answered Oct 05 '22 19:10

hongliang


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.

like image 32
heavyd Avatar answered Oct 05 '22 20:10

heavyd