How to use Preprocessing Directives #define

Description

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

Syntax

The general form for #define:

#define symbol

Example

Example,


#define DEBUGLOG/*from   ww  w  . jav a 2s. c o m*/

using System;

class MainClass
{
    public static void Main()
    {
        #if DEBUGLOG
        Console.WriteLine("In Main - Debug Enabled");
        #else
        Console.WriteLine("In Main - No Debug");
        #endif
    }
}

The code above generates the following result.

Example 2

Use the Conditional attribute with #define


#define USE_METHOD_1/*w  w  w.ja v  a2s  .  c  om*/

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();
  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Custom Types »




C# Class
C# Struct
C# Interface
C# Inheritance
C# Namespace
C# Object
C# Delegate
C# Lambda
C# Event
C# Enum
C# Attribute
C# Generics
C# Preprocessor