Pragma is a compiler directive in C#.This article discusses specifically on #pragma directives in C#.
Compiler directive #Pragma
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
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.
1 |
#pragma checksum "yourfile.cs" "{3673e4ca-6098-4ec1-890f-8fceb2a794a2}" "{012345678AB}" |
This generates a new Checksum for the file your file.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.
Leave a Reply