Preprocessor Directives in C#
Last Updated :
29 Mar, 2019
Preprocessor Directives in C# tell the compiler to process the given information before actual compilation of the program starts. It begins with a hashtag symbol (#) and since these preprocessors are not statements so no semi-colon is appended at the end. The C# compiler does not have a separate preprocessor, yet the directives are processed as if there was one. There cannot be anything else in a line other than the preprocessor directive.
The Preprocessors used in C# are given below:
Preprocessor |
Description |
#define |
To define a Symbol |
#undef |
Removes any definition of a symbol |
#if |
Checks if the symbol evaluates to true |
#endif |
Ends the conditional directive which began with #if |
#else |
If the symbolic value of #if evaluates to false the #else directive statements are executed |
#elif |
Creates a compound conditional directive which is executed if the symbolic value is true |
#error |
Creates a user defined error |
#warning |
Creates a user defined warning |
#line |
Modifies the compiler's default line numbering |
#region |
Specifies a block of code that can be expanded or collapsed |
#endregion |
Specifies the end of a region |
#pragma |
Gives the compiler information for compilation of the file |
#pragma warning |
Used for enabling or disabling warnings |
#pragma checksum |
Creates checksums for source files |
Example 1: Using #define, #if, #else and #endif Let us understand this concept with a few examples. In the code given below, we are using
#define to define a symbol called
shape. So this means that '
shape' evaluates to true. Inside main, we check if
shape exists or not using
#if. Since it does exists and the compiler knows it beforehand so the part
#else will never be executed and is treated as a comment by the compiler. The
#endif is used to indicate the end of the if derective.
csharp
// C# Program to show the use of
// preprocessor directives
// Defining a symbol shape
#define shape
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Preprocessor {
class Program {
static void Main(string[] args)
{
// Checking if symbol shape exists or not
#if (shape)
Console.WriteLine("Shape Exists");
#else
Console.WriteLine("Shape does not Exist");
// Ending the if directive
#endif
}
}
}
Output:
Shape Exists
Example 2 : Using #warning and #define Consider another example. In the code given below, we deliberately remove the definitions of symbols
shape and
shape_. Since the compiler cannot find these it executes the
#else directive. Here, we generate user defined warning and error.
csharp
// C# program to show the Removal of
// definition of shape and shape_
#undef shape_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace preprocessor2
{
class Program
{
static void Main( string[] args )
{
// Checking if shape exists
#if (shape)
Console.WriteLine("Shape Exists");
// Or if shape_ exists
#elif (shape_)
Console.WriteLine("Shape_ Exists" );
#else
// using #warning to display message that
// none of the symbols were found
#warning "No Symbols found"
// Generating user defined error
#error "Check use of preprocessors"
// Ending if
#endif
}
}
}
This code will not compile as there exists an error in the code. Warnings and errors essentially do the same job but the error will stop the compilation process of the code. The visual studio will give you the following result:
Example 3: Using #region and #endregion #region defines a set of instructions as a block of code and this block is compiled all at once by the compiler.
#endregion marks the end of the block. The program below depicts the same.
csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace preprocessor3
{
class Program
{
static void Main( string[] args )
{
char ch = 'y';
// Using #region to define a block of code
#region
if (ch == 'y' || ch == 'Y')
Console.WriteLine( "Value of ch is 'y'" );
else
Console.WriteLine( "Value of ch is unknown" );
// Ends the region
#endregion
}
}
}
Output:
Value of ch is 'y'
Example 4: Using #pragma warning and #pragma checksum In the code below we use the
#pragma warning disable to disable all warnings. Inside main, we generate a user defined warning to check if it has been disabled or not. And
#pragma checksum is used to aid the debugging of the file.
csharp
// C# program to Disables all warnings
#pragma warning disable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Preprocessor4 {
class GFG {
static void Main(string[] args)
{
// Creating a warning
#warning "This is disabled"
// Checksum is used for debugging
// the file in consideration
#pragma checksum "Program.cs"
}
}
}
The error list for the above code:
Similar Reads
C# Interactive Interpreter
This article is designed to showcase the step by step implementation of Interactive C# Interpreter Development by virtue of .NET framework codeDOM APIâs which enables us to dynamically evaluate C# code expressions and statements for testing mini code fragments or scripts more or less like to Python
11 min read
C# Restrictions on Properties
Properties in C# are special class members that provide a flexible mechanism to read, write, or compute the value of a private field. They act as methods called accessors i.e. "get" and "set" methods. Using Properties, we can achieve encapsulation and information hiding. Properties allow controlled
5 min read
C# Identifiers
In programming languages, identifiers are used for identification purposes. Or in other words, identifiers are the user-defined name of the program components. In C#, an identifier can be a class name, method name, variable name, or label. Example: public class GFG { static public void Main () { int
2 min read
Expression-Bodied Members in C#
Expression-bodied members provide a minimal and concise syntax to define properties and methods. It helps to eliminate boilerplate code and helps writing code that is more readable. The expression-bodied syntax can be used when a member's body consists only of one expression. It uses the =>(fat a
7 min read
Introduction to C#
C# (C-sharp) is a modern, object-oriented programming language developed by Microsoft as part of the .NET framework. It was first released in 2000 and it has become one of the most widely used languages for building Windows applications, web services, and more. C# combines the power of C and C++ wit
7 min read
C# | Method Parameters
Methods in C# are generally the block of codes or statements in a program which gives the user the ability to reuse the same code which ultimately saves the excessive use of memory, acts as a time saver and more importantly, it provides better readability of the code. So you can say a method is a co
7 min read
Characteristics of .NET Framework
.NET is a software framework that is designed and developed by Microsoft. The first version of the .Net framework was 1.0 which came in the year 2002. In easy words, it is a virtual machine for compiling and executing programs written in different languages like C#, VB.Net, etc. .NET is the framewor
5 min read
C# - FileInfo Class Methods
In this article, we will explain the FileInfo class, its methods, properties, etc. The System.IO namespace is one of the most important namespaces we used while we are working with files in C#. FileInfo Class:It does not have static methods and it can only be used on instantiated objects. The class
3 min read
How to Execute C# Program on cmd (command-line)?
C# is a general-purpose, modern and object-oriented programming language pronounced as "C sharp". C# is among the languages for Common Language Infrastructure and the current version of C# is version 8.0. C# is a lot similar to Java syntactically and is easy for the users who know C, C++ or Java. Si
3 min read
Main Method in C#
C# applications have an entry point called Main Method. It is the first method which gets invoked whenever an application started and it is present in every C# executable file. The application may be Console Application or Windows Application. The most common entry point of a C# program is static vo
5 min read