Compiler directives are there for almost all languages. Pragma directives are the special instruction to the compiler when it compiles a file. For instance, if you want to your function does not react to some of the warnings that compiler produces, you can use Pragma pre-processor directives.
C# Pragma
There are two types of Pragma support in .NET, Pragma Warning and Pragma Checksum
Pragma Warning
Pragma warning enables you to disable/enable certain compiler warnings that the compiler generates when certain code appears in your code.
The #pragma keyword tells your compiler to display or not to display certain warnings. Meaning, you can enable or disable specified warnings using #pragma keyword.
You need to know warning number to enable/disable that specific warning or warnings.
For example, if you want to disable the XML comments warning(1591) then the code syntax goes as below
#pragma warning disable 1591
Similarly, you can enable warnings
#pragma warning enable 3021
Pragma Checksum
Checksum is a special number that identifies a file.
The .NET debugger generates a checksum of a file and puts in Program Database(PDB file) when it compiles. Hence even though your file is modified when you run, the checksum will always point to right source.
In case of ASP.NET, the computed checksum that is produced in the PDF file is actually points to the one that is generated rather than the actual source. You can use Pragma statement in your asp.net file to generate new checksum for your file.
#pragma checksum "yourfile.cs" "{3673e4ca-6098-4ec1-890f-8fceb2a794a2}" "{012345678AB}"
This generates a new Checksum for the file yourfile.cs and the new checksum will be written to the PDB it generates.
Remember, the hexadecimal value you pass as last argument in #pragma checksum directive should be even, any odd checksum byte will be ignored by the compiler.
Frequently Asked Questions on #Pragma
What is #pragma in C#?
Warning can be disabled or enabled by using the #pragma directive in C# code.
Example,
#pragma warning disable warning-list
ie, (#pragma warning disable CS3021)
#pragma warning restore warning-list
ie, (#pragma warning restore CS3021)
What is #pragma comment ?
A compiler directive that informs to leave a comment in the generated object file follows the #pragma comment. When the linker processes object files, the comment can be read.
Pragma comment syntax is ,
#pragma comment( comment-type [,”stringcomment”] )
What is a directive in C#?
In C#, the preprocessor directives are the compiler commands that have an effect on the compilation process. These code commands instruct how to compile various sections of the code, and how to deal with errors and warnings. The preprocessor directives starts with a # symbol and are limited to one line in length.
It is used for conditional compilations also.
How can we suppress code analysis warning in C#?
To suppress a compiler warning, use Disable directive in Visual Basic & #pragma warning directive in C#.
SuppressMessage attribute is used to suppress a warning at a project level or source file level
Summary
Hope this article on C# Pragma helped in understanding #Pragma C# in detail.Please leave your comments and feedback below.