24.3. The /define compiler option and #undef

Instead of defining a symbol using the #define directive, you can define symbols by passing them in as compiler options. [3] If you are using csc.exe, you can pass in defined symbols like this:

[3] This is extremely useful. You would not want to keep commenting and uncommenting your #define directives, although they are usually conveniently located near the top of the source file.

c:expt>csc /define:DEBUG,SHOW_ON_CONSOLE test.cs

You can use the short form /d: instead of /define:. Symbols to be defined are separated by commas (with no spaces between). Remember that symbols are case sensitive. If your #if directives are expecting the DEBUG symbol, passing in debug (lower case) via the /define option will not satisfy the condition.

You can 'turn off' symbols in the source files by using the #undef directive. You may want to do this if your program execution involves multiple source files, and you want particular symbols to be defined only in certain files. In those source files in which you want the symbols to be undefined, use #undef. Here is an example.

 1: #if DEMO
 2:   #undef DEBUG
 3:   #undef SHOW_EASTER_EGGS
 4: #endif
 5:
 6: using System;
						7:
						8: public class TestClass{
						9:
						10:   public static void Main(){
11: #if DEBUG
12:     Console.WriteLine("Debugging statements");
13: #endif
14: #if SHOW_EASTER_EGG
15:     Console.WriteLine("Greetings from Geekie");
16: #endif
17:   }
						18: }
					

Look at lines 1 – 4. If DEMO is defined, both symbols DEBUG and SHOW_EASTER_EGGS will be undefined, regardless of whether they were passed in via the /define compiler option. There are probably many other creative uses of #define and #undef. A team leader can include rules about symbols in programming guidelines for his development team.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.164.109