Preprocessor 2

image_pdfimage_print

   

/*
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()
        {
#if ALTMAIN
#warning Compiling alternate statement
            Console.WriteLine ("Using alternate Main()");
#elif OTHERMAIN
#warning Compiling other statement
            Console.WriteLine ("Using other Main()");
#else
#warning Compiling main
            Console.WriteLine ("Using Main()");
#endif
        }

#line 200
#if SHOWERROR
        int iVar;
#error This is line 23 but the error report should show line 202
#endif

    }
}


           
          


Demonstrate #elif

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate #elif. 
 
#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."); 
  } 
}

           
          


Demonstrate #else

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate #else. 
 
#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."); 
  } 
}

           
          


Use a symbol expression

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use a symbol expression. 
 
#define EXPERIMENTAL 
#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."); 
  } 
}


           
          


Demonstrate #if, #endif, and #define

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate #if, #endif, and #define. 
 
#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."); 
  } 
}


           
          


#undef, #elif, and #else preprocessor directives

image_pdfimage_print

/*
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
*/

#define DEBUG
#undef DEBUG
#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) { #if DEBUG System.Console.WriteLine("goto myLabel"); #elif PRODUCTION System.Console.WriteLine("counter < 5"); #else System.Console.WriteLine("goto myLabel, counter < 5"); #endif goto myLabel; } System.Console.WriteLine("total = " + total); } } [/csharp]

#define, #if, and #endif preprocessor directives

image_pdfimage_print

/*
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
*/

#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) { #if DEBUG System.Console.WriteLine("goto myLabel"); #endif goto myLabel; } System.Console.WriteLine("total = " + total); } } [/csharp]