Home>

Introduction to this article:

C#preprocessor directives never translate into commands for executable code,But it affects all aspects of the compilation process,Commonly used preprocessor directives are #define, #undef, #if, #elif, #else, #endif, etc. The following is an example of conditional compilation using #define in c#.

Conditional compilation directives in c#are used to conditionally include or exclude parts of a source file.In visual studio, you will see the excluded code is grayed out.

What #define can be used for

1. When planning to release two versions of the code.I.e. the basic version and the enterprise version with more versions,You can use conditional compilation instructions;

2. For example, the same file is used for silverlight, wpf, winform, etc., and debug and release are also considered. Most of the code is the same;

3. Specify whether functions and attributes are compiled into the final product.

#Define usage

Syntax:#define name

Note:the name here is debug, you can also take other names such as dragon

#define debug

Explanation:

1, debug can be seen as a declared variable,But this variable has no real value,#If debug result is true when it exists, otherwise it is false;

2, #define alone makes little sense,Generally used in combination with #if or conditional features;

3. #define must be defined in front of all using namespaces;

4, debug and debug are different,c#is case sensitive.

Third, #define conditional compilation examples

method one,Use #if

#define dragon
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.diagnostics;
namespace conditionalcompilation
{
  class program
  {
    static void main (string [] args)
    {
#if dragon
      console.writeline ("dragon is defined");
#else
      console.writeline ("dragon is not defined");
#endif
      console.readkey ();
    }
  }
}

The output is as follows:

If you comment out //#define dragon, the output is:

Way two,Using conditional attributes

We can isolate some functions,So that they only work if certain environment variables are defined or a value is set,Isolation strategies that use the conditional feature are less error prone than #if/#endif.

#define debug
#define trace
#if (debug &&trace)
#define debugandtrace
#endif
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.diagnostics;
namespace conditionalcompilation
{
  class program
  {
    static void main (string [] args)
    {
      print0 ();
      print1 ();
      print2 ();
      print3 ();
      console.readkey ();
    }
    [conditional ("debug")]
    static void print0 ()
    {
      console.writeline ("debug is defined");
    }
    [conditional ("debug")]
    static void print1 ()
    {
      console.writeline ("debug is defined");
    }
    //This method will be executed only after debug or trace is defined
    //or the relationship
    [conditional ("debug"), conditional ("trace")]
    static void print2 ()
    {
      console.writeline ("debug or trace is defined");
    }
    //This method will only be executed if debug and trace are defined
    //and the relationship
    [conditional ("debugandtrace")]
    static void print3 ()
    {
      console.writeline ("debug and trace is defined");
    }
  }
}

The output is as follows:

Explanation:

1. Debug is not defined in the code, but debug is output because the debug version automatically defines debug. The checkbox in front of the definition of the debug constant (u) under "Project-Right-click-Properties-Build tab-General bar" is selected.Of course you can remove its selected state,This will not output debug.

2. If neither debug nor trace is defined,No debug or trace will be output;only debug and trace are defined,Only debug and trace will be output.

3. You can add multiple attributes to the conditional, such as the sample code [conditional ("debug"), conditional ("trace")], but the relationship between multiple attributes is an OR relationship.That is, either "debug" or "trace" is defined,Then the corresponding method will be executed.

4. If you need to add multiple AND attributes,It is not possible to use conditional directly.Need to be done indirectly with the help of #if/#endif,As in the sample code

#if (debug &&trace)
#define debugandtrace
#endif

Methods that use the conditional attribute are subject to the following restrictions:

1. Conditional methods must be methods in a class declaration or a structure declaration.If you specify the conditional attribute on a method in an interface declaration, a compile-time error will occur;

2. Conditional methods cannot be implementations of interface methods.Otherwise a compile-time error will occur;

3. Conditional methods must have void return type;

4. Conditional methods cannot be marked with the override modifier.However, conditional methods can be marked with a virtual modifier.Overriding methods of such methods are implicitly conditional,It cannot be explicitly marked with the conditional attribute.

There are three ways to set environment variables (or conditional compilation symbols):

1) Use #define definition and #undef to cancel definition,Defined before all using namespaces;

2) Use the compiler command line options (for example,/define:debug), set in the conditional compilation symbol (y) under "Project-Right-Properties-Build tab-General bar" (if multiple ,Can be separated by commas). In debug version, the system has debug and trace set by default;

3) Use the environment variables in the operating system shell (for example, set debug=1).

c
  • Previous Ajax debugging steps under jQuery
  • Next JQuery menu effect two examples explain (3)