Algorithm
-
#define:
- Define a symbolic name or a macro.
-
#undef:
- Undefine a previously defined macro.
-
#if:
- Conditionally compile code if a specified expression evaluates to true.
-
#else:
- Part of a conditional compilation statement; executed if the preceding #if or #elif directive evaluates to false.
-
#elif:
- Part of a conditional compilation statement; allows testing additional conditions if the preceding #if or #elif directive evaluates to false.
-
#endif:
- Ends a block of conditional compilation directives.
-
#warning:
- Generates a warning message during compilation.
-
#error:
- Generates an error message during compilation.
-
#line:
- Lets you modify the compiler's line number and (optionally) the file name reported in error messages.
-
#region:
- Marks the start of a collapsible region in the code editor.
- #endregion:
- Marks the end of a collapsible region.
- #pragma:
- Provides instructions to the compiler or other tools.
- #nullable:
- Specifies whether reference types and value types are non-nullable or nullable.
Code Examples
#1 How to use #if directive in C#?
Code -
C# Programming
#define CSHARP
using System;
namespace Directive
{
class ConditionalDirective
{
public static void Main(string[] args)
{
#if (CSHARP)
Console.WriteLine("CSHARP is defined");
#endif
}
}
}
Copy The Code &
Try With Live Editor
Output
#2 How to use conditional directive (if, elif, else, endif) in C# Programming?
Code -
C# Programming
#define CSHARP
#undef PYTHON
using System;
namespace Directive
{
class ConditionalDirective
{
static void Main(string[] args)
{
#if (CSHARP && PYTHON)
Console.WriteLine("CSHARP and PYTHON are defined");
#elif (CSHARP && !PYTHON)
Console.WriteLine("CSHARP is defined, PYTHON is undefined");
#elif (!CSHARP && PYTHON)
Console.WriteLine("PYTHON is defined, CSHARP is undefined");
#else
Console.WriteLine("CSHARP and PYTHON are undefined");
#endif
}
}
}
Copy The Code &
Try With Live Editor
Output
#3 How to use #warning directive in C# Programming?
Code -
C# Programming
using System;
namespace Directives
{
class WarningDirective
{
public static void Main(string[] args)
{
#if (!CSHARP)
#warning CSHARP is undefined
#endif
Console.WriteLine("#warning directive example");
}
}
}
Copy The Code &
Try With Live Editor
#4 How to use #error directive In C# programming?
Code -
C# Programming
using System;
namespace Directive
{
class Error
{
public static void Main(string[] args)
{
#if (!CSHARP)
#error CSHARP is undefined
#endif
Console.WriteLine("#error directive example");
}
}
}
Copy The Code &
Try With Live Editor
Output
The build failed. Please fix the build errors and run again.
#5 How to use #line directive in C# P[rogramming?
Code -
C# Programming
using System;
namespace Directive
{
class Error
{
public static void Main(string[] args)
{
#line 200 "AnotherProgram.cs"
#warning Actual Warning generated by Program.cs on line 10
}
}
}
Copy The Code &
Try With Live Editor
Output
#6 How to use #region directive in C# Programming?
Code -
C# Programming
using System;
namespace Directive
{
class Region
{
public static void Main(string[] args)
{
#region Hello
Console.WriteLine("Hello");
Console.WriteLine("Hello");
Console.WriteLine("Hello");
Console.WriteLine("Hello");
Console.WriteLine("Hello");
#endregion
}
}
}
Copy The Code &
Try With Live Editor
Output
Hello
Hello
Hello
Hello
#7 How to use #pragma directive in C# programming?
Code -
C# Programming
using System;
namespace Directive
{
class Error
{
public static void Main(string[] args)
{
#pragma warning disable
#warning This is a warning 1
#pragma warning restore
#warning This is a warning 2
}
}
}
Copy The Code &
Try With Live Editor
Output
Demonstration
C# Program Preprocessor directives