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]

Demonstrates the use of a conditional method

image_pdfimage_print

/*
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
//
#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"); } } } } [/csharp]

#undef Marco

image_pdfimage_print
   
 


#define MYOWN
#define DEBUG
#undef VERBOSE
#define PROGRAMMER_IS_BRIAN
#undef PROGRAMMER_IS_DAVE

using System;

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

#if MYOWN &amp;&amp; (VERBOSE || DEBUG)
        Console.WriteLine("Continuing, Author");
#elif !MYOWN &amp;&amp; (VERBOSE || DEBUG)
   Console.WriteLine("Continuing?);
#endif

#if PROGRAMMER_IS_BRIAN || PROGRAMMER_IS_DAVE
#warning Execution may vary depending on programmer.
#endif

#if PROGRAMMER_IS_DAVE
#error Something you did broke this code.
#endif
    }
}

    


precompile marco: define, undef, elif, endif

image_pdfimage_print
   
 

#define MYOWN
#define DEBUG
#undef VERBOSE

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