Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sure. These refer to <em>conditional compilation symbols</em> which can be defined at compile-time and which control what code is actually built. Here's an example:</p> <pre><code>using System; class Test { static void Main() { #if FOO Console.WriteLine("FOO was defined"); #endif #if BAR Console.WriteLine("BAR was defined"); #endif } } </code></pre> <p>If you compile this with</p> <pre><code>csc Test.cs </code></pre> <p>It won't print anything. If you compile it with</p> <pre><code>csc Test.cs /D:FOO </code></pre> <p>then it will print "FOO was defined" - and obviously the same is true for BAR.</p> <p>Note that these aren't the same as C++ macros - a symbol is either defined or not; it doesn't have a "replacement value" as such.</p> <p>In Visual Studio, you specify which symbols should be defined in the Build tab of the project properties. Additionally, at the very start of the file you can explicitly define and undefine symbols:</p> <pre><code>#define FOO #undef BAR </code></pre> <p>This can be important when calling methods decorated with <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.conditionalattribute.aspx" rel="noreferrer"><code>ConditionalAttribute</code></a> - such calls are ignored by the compiler if the appropriate symbol isn't defined. So if you wanted to make sure that all your <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.debug.print.aspx" rel="noreferrer"><code>Debug.Print</code></a> calls came through even if you hadn't defined the DEBUG symbol for the rest of the project, you could use:</p> <pre><code>#define DEBUG ... Debug.Print("Foo"); </code></pre> <p>Personally, I don't use all this very much. Aside from anything else, it makes it easier to understand the code if you know that it will all be compiled and run at execution time.</p> <p>EDIT: Just to clarify a little on terminology - <code>#if</code>, <code>#line</code>, <code>#pragma</code> etc are all preprocessor directives; FOO and BAR (in this case) are the conditional compilation symbols.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload