codescracker


c#

C# Preprocessors



« Previous Tutorial Next Tutorial »


Preprocessor directives in C#, simply give instruction to the C# compiler to pre-process the information before the actual program compilation starts. All the preprocessor directives begins with hash (#).

C# Preprocessor Directive Lists

Here, the following table lists the preprocessor directives available in C#:

Preprocessor Directive Meaning
#define This preprocessor directive simply defines a sequence of characters, called as symbol
#if This preprocessor directive just allows to test symbol(s) to see if they evaluate to be true or not
#endif This preprocessor directive is used to specify the end of a conditional directive
#undef This preprocessor directive is used to un-define a symbol in a C# program
#else This preprocessor directive is used to create a compound conditional directive, along with #if preprocessor directive
#error This preprocessor directive is used to generate an error from a specific location in your program
#elif This preprocessor directive is used to create a compound conditional directive
#line This preprocessor directive lets you to modify the compiler's line number and the filename output for the errors and warnings
#region This preprocessor directive lets you to specify a block of code that you can expand/collapse when using the outlining feature of the Visual Studio Code Editor
#endregion This preprocessor directive marks the end of a #region block
#warning This preprocessor directive allows you to generate a level one warning from a specific location in your program

C# #define Preprocessor

The #define preprocessor directive in C#, allows you to create symbolic constants. Here is the general form to use #define in C# program:

#define symbol

Here is an example:

/* C# Preprocessor - C# #define Preprocessor - Example Program */
		
#define PI 
using System;
namespace PreprocessorExample
{
   class PreprocessorClass
   {
      static void Main(string[] args)
      {
         #if(PI)
            Console.WriteLine("PI is defined");
         #else
            Console.WriteLine("PI is not defined");
         #endif
         Console.ReadKey();
      }
   }
}

When we compile and run the above program, we will get the following output:

PI is defined

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test