Algorithm


  1. #define:

    • Define a symbolic name or a macro.
  2. #undef:

    • Undefine a previously defined macro.
  3. #if:

    • Conditionally compile code if a specified expression evaluates to true.
  4. #else:

    • Part of a conditional compilation statement; executed if the preceding #if or #elif directive evaluates to false.
  5. #elif:

    • Part of a conditional compilation statement; allows testing additional conditions if the preceding #if or #elif directive evaluates to false.
  6. #endif:

    • Ends a block of conditional compilation directives.
  7. #warning:

    • Generates a warning message during compilation.
  8. #error:

    • Generates an error message during compilation.
  9. #line:

    • Lets you modify the compiler's line number and (optionally) the file name reported in error messages.
  10. #region:

  • Marks the start of a collapsible region in the code editor.
  1. #endregion:
  • Marks the end of a collapsible region.
  1. #pragma:
  • Provides instructions to the compiler or other tools.
  1. #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

x
+
cmd
CSHARP is defined

#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

x
+
cmd
CSHARP is defined, PYTHON is undefined

#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

x
+
cmd
Program.cs(10,24): error CS1029: #error: 'CSHARP is undefined' [/home/myuser/csharp/directives-project/directives-project.csproj]
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

x
+
cmd
AnotherProgram.cs(200,22): warning CS1030: #warning: 'Actual Warning generated by Program.cs on line 10' [/home/myuser/csh arp/directive-project/directive-project.csproj]

#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

x
+
cmd
Hello
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

x
+
cmd
Program.cs(12,22): warning CS1030: #warning: 'This is a warning 2' [/home/myuser/csharp/directive-project/directive-project.csproj]
Advertisements

Demonstration


C# Program Preprocessor directives