Sunday, June 05, 2011

Compiler directive #Pragma reference

Compiler directives is always a fun for any language. There are lots of talks around different blogs on compiler directives but there are hardly any talks around my favorite #pragma directives. So lets talk what it is and what it is capable of.
In general, Pragma directives are special instruction to the compiler when it compiles a file. Say 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.

.NET supports two types of Pragma

1. Pragma Warning
2. Pragma Checksum

Pragma Warning

Pragma warning allows you to disable / enable certain warnings that the compiler produces when certain code appears on your code.

For example :

private static void Main(string[] args)
{
int x = 10;
}
Now if you want to disable this warning for the code you write, you can wrap around your code in pragma statement like below :

private static void Main(string[] args)
{
# pragma warning disable 0219
int x = 10;
#pragma warning restore 0219
}

Read more: Daily .Net Tips