#define, #if, and #endif preprocessor directives

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

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

   
 


#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

   
 

#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
    }
}

    


line number

   
 

using System;

class Operators {
    static void Main() {
#line 300
#warning Something happened.
#warning Something else happened.
#line 400 "someotherfile.cs"
#warning Something happened later.
#line default
#warning Nothing else will happen.
    }
}

    


Unsafe Context

/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
// 36 – Deeper into C#Unsafe Context
// copyright 2000 Eric Gunnerson
// file=unsafe.cs
// compile with: csc /unsafe /o+ unsafe.cs
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

public class UnsafeContext
{
const int iterations = 20000; // # to do copy
const int points = 1000; // # of points in array
const int retryCount = 5; // # of times to retry

public delegate Point[] CloneFunction(Point[] a);

public static void TimeFunction(Point[] arr,
CloneFunction func, string label)
{
Point[] arrCopy = null;
long start;
long delta;
double min = 5000.0d; // big number;

// do the whole copy retryCount times, find fastest time
for (int retry = 0; retry < retryCount; retry++) { start = Counter.Value; for (int iterate = 0; iterate < iterations; iterate++) arrCopy = func(arr); delta = Counter.Value - start; double result = (double) delta / Counter.Frequency; if (result < min) min = result; } Console.WriteLine("{0}: {1:F3} seconds", label, min); } public static void Main() { Console.WriteLine("Points, Iterations: {0} {1}", points, iterations); Point[] arr = new Point[points]; for (int index = 0; index < points; index++) arr[index] = new Point(3, 5); TimeFunction(arr, new CloneFunction(Point.ClonePointArrayMemcpy), "Memcpy"); TimeFunction(arr, new CloneFunction(Point.ClonePointArrayUnsafe), "Unsafe"); TimeFunction(arr, new CloneFunction(Point.ClonePointArray), "Baseline"); } } class Counter { public static long Frequency { get { long freq = 0; QueryPerformanceFrequency(ref freq); return freq; } } public static long Value { get { long count = 0; QueryPerformanceCounter(ref count); return count; } } [System.Runtime.InteropServices.DllImport("KERNEL32", CharSet=System.Runtime.InteropServices.CharSet.Auto)] private static extern bool QueryPerformanceCounter( ref long lpPerformanceCount); [System.Runtime.InteropServices.DllImport("KERNEL32", CharSet=System.Runtime.InteropServices.CharSet.Auto)] private static extern bool QueryPerformanceFrequency( ref long lpFrequency); } public struct Point { public Point(int x, int y) { this.x = x; this.y = y; } // safe version public static Point[] ClonePointArray(Point[] a) { Point[] ret = new Point[a.Length]; for (int index = 0; index < a.Length; index++) ret[index] = a[index]; return(ret); } // unsafe version using pointer arithmetic unsafe public static Point[] ClonePointArrayUnsafe(Point[] a) { Point[] ret = new Point[a.Length]; // a and ret are pinned; they cannot be moved by // the garbage collector inside the fixed block. fixed (Point* src = a, dest = ret) { Point* pSrc = src; Point* pDest = dest; for (int index = 0; index < a.Length; index++) { *pDest = *pSrc; pSrc++; pDest++; } } return(ret); } // import CopyMemory from kernel32 [DllImport("kernel32.dll")] unsafe public static extern void CopyMemory(void* dest, void* src, int length); // unsafe version calling CopyMemory() unsafe public static Point[] ClonePointArrayMemcpy(Point[] a) { Point[] ret = new Point[a.Length]; fixed (Point* src = a, dest = ret) { CopyMemory(dest, src, a.Length * sizeof(Point)); } return(ret); } public override string ToString() { return(String.Format("({0}, {1})", x, y)); } int x; int y; } [/csharp]