## C# Tutorial

Preprocessor directives are a block of statements that are handled before the actual compilation process starts. They give some instructions to the compiler on the compilation process, error or warning handling etc.Preprocessor directives begin with a hash symbol (#) and do not contain the semicolon at the end as they are not statements. They are instead terminated by a new line.Some of the preprocessor directives in C# are given as follows:Table: Preprocessor directives in C#Preprocessor DirectiveDescription#ifThe #if directive compiles the code between the directives only if the specified symbol is defined.#elseThe #else directive allows the user to  create a compound conditional directive.  If none of the expressions in the preceding #if or the #elif directives evaluate to true, then the compiler evaluates all the code between #else and the subsequent #endif.#elifThe #elif is a compound conditional directive that is evaluated if neither the preceding #if nor any preceding optional #elif directive expressions evaluate to true.#endifThe #endif directive specifies the end of a conditional directive which began with the #if directive.#defineThe #define directive is used to define a symbol, which is a sequence of characters.#undefThe #undef directive lets the user to undefine a symbol.#warningThe #warning directive is used to generate a CS1030 level one compiler warning from a specific location in the code.#errorThe #error directive allows the user to  generate a CS1029 user-defined error from a specific location in your code.#lineThe #line directive lets the user modify the compiler's line numbering and also optionally the file name output for errors and warnings#regionThe #region directive allows the user to specify a block of code that be expanded or collapsed when using the outlining feature of the Visual Studio Code Editor.#endregionThe #endregion directive marks the end of a #region block.#pragmaThe #pragma directive provides the compiler special instructions for the compilation of the file in which it appears.#pragma warningThe #pragma warning directive can enable or disable certain warnings.#pragma checksumThe #pragma checksum directive generates checksums for source files to help with debugging ASP.NET pages.Some of the examples for various preprocessor directives are given as follows:The #define Preprocessor DirectiveThe #define directive is used to define a symbol, which is a sequence of characters. The symbols that are defined using the #define directive evaluate to true when used with the #if directive.A program that demonstrates the #define directive is given as follows:Source Code: Program that demonstrates the #define directive in C##define TEST using System; namespace PreprocessorDirectivesDemo {   class Example   {      static void Main(string[] args)      {         #if (TEST)            Console.WriteLine("The TEST is defined");         #else            Console.WriteLine("The TEST is not defined");         #endif      }   } }The output of the above program is as follows:The TEST is definedThe #undef Preprocessor DirectiveThe #undef directive allows the user to undefine a symbol. The symbols that are undefined using the #undef directive evaluate to false when used with the #if directive.A program that demonstrates the #undef directive is given as follows:Source Code: Program that demonstrates the #undef directive in C##undef TEST using System; namespace PreprocessorDirectivesDemo {   class Example   {      static void Main(string[] args)      {         #if (TEST)            Console.WriteLine("The TEST is defined");         #else            Console.WriteLine("The TEST is not defined");         #endif      }   } }The output of the above program is as follows:The TEST is not definedThe #if Preprocessor DirectiveThe #if directive compiles the code between the directives only if the specified symbol is defined. It is basically used to create a conditional directive. This means that the code that is inside the #if directive is only compiled if the expression tested with the #if directive evaluates to true. The #endif directive always the if directive.A program that demonstrates the #if directive is given as follows:Source Code: Program that demonstrates the #if directive in C##define TEST using System; namespace PreprocessorDirectivesDemo {   class Example   {      static void Main(string[] args)      {         #if (TEST)            Console.WriteLine("The TEST is defined");         #endif      }   } }The output of the above program is as follows:The TEST is definedThe #elif Preprocessor DirectiveThe #elif is a compound conditional directive that is evaluated if neither the preceding #if nor any preceding optional #elif directive expressions evaluate to true. This preprocessor directive is used when multiple expressions are tested.A program that demonstrates the #elif directive is given as follows:Source Code: Program that demonstrates the #elif directive in C##undef TEST1 #define TEST2 using System; namespace PreprocessorDirectivesDemo {   class Example   {      static void Main(string[] args)      {         #if (TEST1 && TEST2) Console.WriteLine("Both TEST1 and TEST2 are defined"); #elif (TEST1 && !TEST2) Console.WriteLine("TEST1 is defined and TEST2 is undefined"); #elif (!TEST1 && TEST2) Console.WriteLine("TEST1 is undefined and TEST2 is defined"); #else Console.WriteLine("Both TEST1 and TEST2 are undefined"); #endif      }   } }The output of the above program is as follows:TEST1 is undefined and TEST2 is definedThe #warning Preprocessor DirectiveThe #warning directive is used to generate a CS1030 level one compiler warning from a specific location in the code.A program that demonstrates the #warning directive is given as follows:Source Code: Program that demonstrates the #warning directive in C#using System; namespace PreprocessorDirectivesDemo {   class Example   {      static void Main(string[] args)      {         #if (!TEST) #warning TEST is undefined #endif      }   } }The output of the above program is as follows:main.cs(11,0): warning CS1030: #warning: TEST is undefined'

# Preprocessor Directives in C#

Preprocessor directives are a block of statements that are handled before the actual compilation process starts. They give some instructions to the compiler on the compilation process, error or warning handling etc.

Preprocessor directives begin with a hash symbol (#) and do not contain the semicolon at the end as they are not statements. They are instead terminated by a new line.

Some of the preprocessor directives in C# are given as follows:

Table: Preprocessor directives in C#

Preprocessor DirectiveDescription
#ifThe #if directive compiles the code between the directives only if the specified symbol is defined.
#elseThe #else directive allows the user to  create a compound conditional directive.  If none of the expressions in the preceding #if or the #elif directives evaluate to true, then the compiler evaluates all the code between #else and the subsequent #endif.
#elifThe #elif is a compound conditional directive that is evaluated if neither the preceding #if nor any preceding optional #elif directive expressions evaluate to true.
#endifThe #endif directive specifies the end of a conditional directive which began with the #if directive.
#defineThe #define directive is used to define a symbol, which is a sequence of characters.
#undefThe #undef directive lets the user to undefine a symbol.
#warningThe #warning directive is used to generate a CS1030 level one compiler warning from a specific location in the code.
#errorThe #error directive allows the user to  generate a CS1029 user-defined error from a specific location in your code.
#lineThe #line directive lets the user modify the compiler's line numbering and also optionally the file name output for errors and warnings
#regionThe #region directive allows the user to specify a block of code that be expanded or collapsed when using the outlining feature of the Visual Studio Code Editor.
#endregionThe #endregion directive marks the end of a #region block.
#pragmaThe #pragma directive provides the compiler special instructions for the compilation of the file in which it appears.
#pragma warningThe #pragma warning directive can enable or disable certain warnings.
#pragma checksumThe #pragma checksum directive generates checksums for source files to help with debugging ASP.NET pages.

Some of the examples for various preprocessor directives are given as follows:

## The #define Preprocessor Directive

The #define directive is used to define a symbol, which is a sequence of characters. The symbols that are defined using the #define directive evaluate to true when used with the #if directive.

A program that demonstrates the #define directive is given as follows:

Source Code: Program that demonstrates the #define directive in C#

#define TEST
using System;
namespace PreprocessorDirectivesDemo
{
class Example
{
static void Main(string[] args)
{
#if (TEST)
Console.WriteLine("The TEST is defined");
#else
Console.WriteLine("The TEST is not defined");
#endif
}
}
}

The output of the above program is as follows:

The TEST is defined

## The #undef Preprocessor Directive

The #undef directive allows the user to undefine a symbol. The symbols that are undefined using the #undef directive evaluate to false when used with the #if directive.

A program that demonstrates the #undef directive is given as follows:

Source Code: Program that demonstrates the #undef directive in C#

#undef TEST
using System;
namespace PreprocessorDirectivesDemo
{
class Example
{
static void Main(string[] args)
{
#if (TEST)
Console.WriteLine("The TEST is defined");
#else
Console.WriteLine("The TEST is not defined");
#endif
}
}
}

The output of the above program is as follows:

The TEST is not defined

## The #if Preprocessor Directive

The #if directive compiles the code between the directives only if the specified symbol is defined. It is basically used to create a conditional directive. This means that the code that is inside the #if directive is only compiled if the expression tested with the #if directive evaluates to true. The #endif directive always the if directive.

A program that demonstrates the #if directive is given as follows:

Source Code: Program that demonstrates the #if directive in C#

#define TEST
using System;
namespace PreprocessorDirectivesDemo
{
class Example
{
static void Main(string[] args)
{
#if (TEST)
Console.WriteLine("The TEST is defined");
#endif
}
}
}

The output of the above program is as follows:

The TEST is defined

## The #elif Preprocessor Directive

The #elif is a compound conditional directive that is evaluated if neither the preceding #if nor any preceding optional #elif directive expressions evaluate to true. This preprocessor directive is used when multiple expressions are tested.

A program that demonstrates the #elif directive is given as follows:

Source Code: Program that demonstrates the #elif directive in C#

#undef TEST1
#define TEST2
using System;
namespace PreprocessorDirectivesDemo
{
class Example
{
static void Main(string[] args)
{
#if (TEST1 && TEST2)
Console.WriteLine("Both TEST1 and TEST2 are defined");
#elif (TEST1 && !TEST2)
Console.WriteLine("TEST1 is defined and TEST2 is undefined");
#elif (!TEST1 && TEST2)
Console.WriteLine("TEST1 is undefined and TEST2 is defined");
#else
Console.WriteLine("Both TEST1 and TEST2 are undefined");
#endif
}
}
}

The output of the above program is as follows:

TEST1 is undefined and TEST2 is defined

## The #warning Preprocessor Directive

The #warning directive is used to generate a CS1030 level one compiler warning from a specific location in the code.

A program that demonstrates the #warning directive is given as follows:

Source Code: Program that demonstrates the #warning directive in C#

using System;
namespace PreprocessorDirectivesDemo
{
class Example
{
static void Main(string[] args)
{
#if (!TEST)
#warning TEST is undefined
#endif
}
}
}

The output of the above program is as follows:

main.cs(11,0): warning CS1030: #warning: TEST is undefined'

very satisfied!!

#### jean-Francois Michaud

Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).

#### Kshitiz

Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz

#### Ed

The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation

## Swift Tutorial

Introduction to Swift Tutorial
13542
Swift Tutorial

Introduction to Swift Tutorial

R Programming

Python Tutorial
5394
Python Tutorial

Python Tutorial