Csharp/C Sharp/Language Basics/Preprocessor Directives

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

#define, #if, and #endif preprocessor directives

<source lang="csharp"> /* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110

  • /

/*

 Example4_16.cs illustrates the use of the
 #define, #if, and #endif preprocessor directives
  • /
  1. define DEBUG

public class Example4_16 {

 public static void Main()
 {
   int total = 0;
   int counter = 0;
   myLabel:
   counter++;
   total += counter;
   System.Console.WriteLine("counter = " + counter);
   if (counter < 5)
   {
  1. if DEBUG
     System.Console.WriteLine("goto myLabel");
  1. endif
     goto myLabel;
   }
   System.Console.WriteLine("total = " + total);
 }

}


      </source>


Demonstrate #elif

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// Demonstrate #elif.

  1. define RELEASE

using System;

public class Test5 {

 public static void Main() { 
    
   #if EXPERIMENTAL 
     Console.WriteLine("Compiled for experimental version."); 
   #elif RELEASE 
     Console.WriteLine("Compiled for release."); 
   #else 
     Console.WriteLine("Compiled for internal testing."); 
   #endif 

   #if TRIAL && !RELEASE 
      Console.WriteLine("Trial version."); 
   #endif 
  
   Console.WriteLine("This is in all versions."); 
 } 

}

      </source>


Demonstrate #else

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// Demonstrate #else.

  1. define EXPERIMENTAL

using System;

public class Test4 {

 public static void Main() { 
    
   #if EXPERIMENTAL 
     Console.WriteLine("Compiled for experimental version."); 
   #else 
     Console.WriteLine("Compiled for release."); 
   #endif 

   #if EXPERIMENTAL && TRIAL 
      Console.Error.WriteLine("Testing experimental trial version."); 
   #else 
      Console.Error.WriteLine("Not experimental trial version."); 
   #endif 
  
   Console.WriteLine("This is in all versions."); 
 } 

}

      </source>


Demonstrate #if, #endif, and #define

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// Demonstrate #if, #endif, and #define.

  1. define EXPERIMENTAL

using System;

public class Test1 {

 public static void Main() { 
    
   #if EXPERIMENTAL 
     Console.WriteLine("Compiled for experimental version."); 
   #endif 
  
   Console.WriteLine("This is in all versions."); 
 } 

}


      </source>


Demonstrates the use of a conditional method

<source lang="csharp"> /* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794

  • /

// CondMeth.cs -- demonstrates the use of a conditional method // // Compile this program with the following command line: // C:>csc CondMeth.cs //

  1. define MY_CONDITION

using System; using System.Diagnostics; namespace nsConditional {

   public class CondMeth
   {
       static public void Main ()
       {
           clsTest test = new clsTest(42);
           test.ShowValue ();
       }
   }
   class clsTest
   {
       public clsTest (int num)
       {
           m_Num = num;
       }
       int m_Num;
       [Conditional("MY_CONDITION")]
       public void ShowValue()
       {
           if (m_Num < 50)
           {
               Console.WriteLine (m_Num + " is less than 50");
           }
       }
   }

}


      </source>


line number

<source lang="csharp"> using System; class Operators {

   static void Main() {
  1. line 300
  2. warning Something happened.
  3. warning Something else happened.
  4. line 400 "someotherfile.cs"
  5. warning Something happened later.
  6. line default
  7. warning Nothing else will happen.
   }

}

</source>


precompile marco: define, undef, elif, endif

<source lang="csharp">

  1. define MYOWN
  2. define DEBUG
  3. undef VERBOSE

using System; class Operators {

   static void Main() {
  1. if MYOWN
       Console.WriteLine("Hi!");
  1. elif VERBOSE
  Console.WriteLine("Program Starting?);
  1. endif
       int a = 10, b = 5;
  1. if DEBUG
       Console.WriteLine("a={0}, b={1}", a, b);
  1. endif
  2. if MYOWN && (VERBOSE || DEBUG)
       Console.WriteLine("Continuing, Author");
  1. elif !MYOWN && (VERBOSE || DEBUG)
  Console.WriteLine("Continuing?);
  1. endif
   }

}

</source>


Preprocessor 2

<source lang="csharp"> /* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794

  • /

namespace nsPreProc {

   using System;
   public class PreProc
   {
       static public void Main()
       {
  1. if ALTMAIN
  2. warning Compiling alternate statement
           Console.WriteLine ("Using alternate Main()");
  1. elif OTHERMAIN
  2. warning Compiling other statement
           Console.WriteLine ("Using other Main()");
  1. else
  2. warning Compiling main
           Console.WriteLine ("Using Main()");
  1. endif
       }
  1. line 200
  2. if SHOWERROR
       int iVar;
  1. error This is line 23 but the error report should show line 202
  2. endif
   }

}


      </source>


#undef, #elif, and #else preprocessor directives

<source lang="csharp"> /* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110

  • /

/*

 Example4_17.cs illustrates the use of the
 #undef, #elif, and #else preprocessor directives
  • /
  1. define DEBUG
  2. undef DEBUG
  3. define PRODUCTION

public class Example4_17 {

 public static void Main()
 {
   int total = 0;
   int counter = 0;
   myLabel:
   counter++;
   total += counter;
   System.Console.WriteLine("counter = " + counter);
   if (counter < 5)
   {
  1. if DEBUG
     System.Console.WriteLine("goto myLabel");
  1. elif PRODUCTION
     System.Console.WriteLine("counter < 5");
  1. else
     System.Console.WriteLine("goto myLabel, counter < 5");
  1. endif
     goto myLabel;
   }
   System.Console.WriteLine("total = " + total);
 }

}

      </source>


#undef Marco

<source lang="csharp">

  1. define MYOWN
  2. define DEBUG
  3. undef VERBOSE
  4. define PROGRAMMER_IS_BRIAN
  5. undef PROGRAMMER_IS_DAVE

using System; class Preprocessor {

   static void Main() {
  1. if MYOWN
       Console.WriteLine("Hi Author!");
  1. elif VERBOSE
  Console.WriteLine("Program Starting?);
  1. endif
       int a = 10, b = 5;
  1. if DEBUG
       Console.WriteLine("a={0}, b={1}", a, b);
  1. endif
  2. if MYOWN && (VERBOSE || DEBUG)
       Console.WriteLine("Continuing, Author");
  1. elif !MYOWN && (VERBOSE || DEBUG)
  Console.WriteLine("Continuing?);
  1. endif
  2. if PROGRAMMER_IS_BRIAN || PROGRAMMER_IS_DAVE
  3. warning Execution may vary depending on programmer.
  4. endif
  5. if PROGRAMMER_IS_DAVE
  6. error Something you did broke this code.
  7. endif
   }

}

</source>


Use a symbol expression

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// Use a symbol expression.

  1. define EXPERIMENTAL
  2. define TRIAL

using System;

public class Test2 {

 public static void Main() { 
    
   #if EXPERIMENTAL 
     Console.WriteLine("Compiled for experimental version."); 
   #endif 

   #if EXPERIMENTAL && TRIAL 
      Console.Error.WriteLine("Testing experimental trial version."); 
   #endif 
  
   Console.WriteLine("This is in all versions."); 
 } 

}


      </source>


Use marco to define flag variable

<source lang="csharp">

  1. define DEBUGGING

using System; class Starter {

  1. if DEBUGGING
   static void OutputLocals() {
       Console.WriteLine("debugging...");
   }
  1. endif
   static void Main() {
  1. if DEBUGGING
       OutputLocals();
  1. endif
   }

}

</source>