Csharp/CSharp Tutorial/Preprocessing Directives/define

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

A list of preprocessor commands available in C#:

#define
#undef
#if
#else
#elif
#endif
#line
#error
#warning
#region
  
#pragma

Preprocessing Directives: define

  1. The #define directive defines a character sequence called a symbol.
  2. The existence or nonexistence of a symbol can be determined by #if or #elif

The general form for #define:


#define symbol

Use a symbol expression.

#define AAA 
#define BBB 
 
using System; 
 
class MainClass { 
  public static void Main() { 
     
    #if AAA 
      Console.WriteLine("Compiled for AAA version."); 
    #endif 
 
    #if AAA && BBB 
       Console.Error.WriteLine("Testing AAA and BBB version."); 
    #endif 
   
    Console.WriteLine("This is in all versions."); 
  } 
}
Compiled for AAA version.
Testing AAA and BBB version.
This is in all versions.

Use the Conditional attribute with define

#define USE_METHOD_1
using System;
using System.Diagnostics;
class MainClass
{
  [Conditional("USE_METHOD_1")]
  public static void Method1()
  {
    Console.WriteLine("In Method 1");
  }
  public static void Main() 
  {
      Console.WriteLine("In Main");
    Method1();
  }
}
In Main
In Method 1