blog

Home / DeveloperSection / Blogs / Preprocessor Directives in C#

Preprocessor Directives in C#

Amit Singh4346 14-Jan-2011

Preprocessor directives are instruction or command. This is the simplymacro (The C preprocessor is a macro processor that is usedautomatically by the C compiler to transform your program before actualcompilation. It is called a macro processor because it allows you to definemacros) term which is the part of c, andc++ then these concept adopt in c#. C# contains set of directives which tellsthe complier to evaluate it.

“A preprocessordirective must be the only instruction on a line. “

There are followingset of directives in c#:

·        #if

·        #else

·        #elif

·        #endif

·        #define

·        #undef

·        #warning

·        #error

·        #line

·        #region

·        #endregion

Forexample
#define india //defineddirective in start of the program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
#if (india)
          Console.WriteLine("Definethe india");
#else
         Console.WriteLine("Not defined the india");
#endif
          Console.ReadLine();
        }
    }
}
 


Output:


Define the india
Note: directives is always defined at thestart of the program.


Updated 18-Sep-2014

Leave Comment

Comments

Liked By