Pragma Directive in C# With Visual Studio 2015

Introduction

C# has a feature known as the #pragma directive. Sometimes when we write code in C# the compiler issues some warnings. Programmers usually ignore warnings because warnings do not prevent the execution of the program. If there are any warnings in our program there is however no effect in the code by those warnings. C# provides a feature known as the #pragma feature to remove the warnings. Sometimes we declare a variable but do not use the variable anywhere in the entire program so when we debug our code the compiler gives a warning so using the #pragma we can disable these types of warnings.

  • The variable is declared but never be used.
  • Unreachable code detected.
  • Do not ignore method rules.
  • An event is never used.

There are many warnings like the preceding.

#pragma directive

It is a very powerful directive to remove the warnings. When the compiler shows some warnings when we debug our code then we can disable all the warnings or any single warning. Here we can also remove a specific warning with its name or the code because warnings ususally result from the C# code, like BC40008, CS1030, CS3021, CS0162, CS0067 and many more depending on the code. So we can handle these warnings using the #pragma directive. So here we discuss the #pragma directive and how it works in C# code. Using the #pragma directive we can disable and restore the warnings depending on our requirements. If we did not specify the warning in our pragma code then it will automatically remove all the warnings. So if you want to remove all the warnings then only write it like this:

#pragma disable warning

There are the following two types of pragmas available in C#:

  • Pragma warning
  • Pragma checksum

Now we check why warnings exist from our code. Basically the main reason is that sometimes we make small errors in our code that do not effect our code but the compiler shows a warning.

Syntax for #pragma

If we want to do disable the warnings then we use this syntax:

  1. #pragma warning disable <warning code/warning name>  
  2. {      
  3.     //code in which warning present  
  4. }  
If we want to restore any warnings in our code then we use the following syntax:
  1. #pragma warning restore <warning code/warning name>  
  2. {  
  3.    //code in which warning present  
  4. }  
Here is an example in which we have warnings.

Code

  1. using System;  
  2. class warning  
  3. {  
  4.    public static void Main()  
  5.     {  
  6.         string name;  //warning::- variable name declared but not used.warning code CS0168  
  7.   
  8.         while (false)   /* warning::- the result of expression is always false science a value  
  9.                         of type int is never equal to null of type int? warning code CS0472*/  
  10.                               
  11.         {  
  12.             int value = 5;   
  13.   
  14.             if (value == null)  //warning::-Unreachable code detected.warning code CS0162  
  15.             {  
  16.               //write code according to your need  
  17.             }  
  18.              
  19.         }  
  20.         Console.WriteLine("hi rizwan...!!!!!");  
  21.         Console.Read();  
  22.   
  23.     }  
  24. }  
When we make this code there are three warnings in this code that are shown in the picture.



Warnings as described are shown in the output window as in the image. Here you can read about the warnings.



But when we debug this code and execute it then it will run successfully and provide the right output.

Output



So one thing is clear here. Warnings do not prevent the code to produce the output, but it tells us what we do unnecessarily and tells us about incorrect declarations in our code as was indicated in the preceding program.

Now here we have the code by which we learn how to remove or disable warnings from our code using the #pragma directive.

Code
  1. using System;  
  2.   
  3. class warning  
  4. {  
  5.     public static void Main()  
  6.     {  
  7. #pragma warning disable CS0168  
  8.         string name;  
  9.   
  10.         while (false)  
  11.         {  
  12. #pragma warning disable CS0162  
  13. #pragma warning disable CS0472  
  14.             int value = 5;  
  15.             if (value == null)  
  16.             {  
  17.                 //write code according to your need  
  18.             }  
  19.   
  20.         }  
  21.         Console.WriteLine("hi rizwan...!!!!!");  
  22.         Console.Read();  
  23.   
  24.     }  
  25. }  
Here we can see in the output window there are no warnings because here we manage the warnings using the #pragma.



Output



Now one thing more is if we want to show some warnings and want to hide some warnings then a #pragma also has the feature known as the restore warning. Let us see that in an example.

Code
  1. using System;  
  2.   
  3. class warning  
  4. {  
  5.     public static void Main()  
  6.     {  
  7. #pragma warning disable CS0168  
  8.         string name;  
  9.   
  10.         while (false)  
  11.         {  
  12. #pragma warning restore CS0162 // by help of this we show the warnings in output window  
  13. #pragma warning disable CS0472  
  14.             int value = 5;  
  15.             if (value == null)  
  16.             {  
  17.                 //write code according to your need  
  18.             }  
  19.   
  20.         }  
  21.         Console.WriteLine("hi rizwan...!!!!!");  
  22.         Console.Read();  
  23.   
  24.     }  
  25. }  
Now in the output window we saw a warning that we restore in our code, but it also provides the same output without any errors.



Output



So here we learn about how to manage the warnings in our code using the the #pragma directive.

pragma checksum

pragma checksum is a very important part of the pragma. Using pragma checksum we generate a unique something but not by which we make a file identifiable. It works just like a primary key concept. When we debug our code then the debugger creates a checksum for the file and and store this checksum into program database file. In Future you might modify your file depending on your needs but when you compile it the pragma checksum always finds the appropriate file or source. For more information about the pragma checksum then please go to the link: #pragma checksum (C# Reference).

Summary

This article exlains how to manage warnings using the #pragma directive to disable and restore. Using the #pragma directive we can handle a single warning or multiple warnings with a single statement. Here we can also specify two or more warnings separted by a "," (comma). There is one thing to remember. If you provide the wrong warning but not with the #pragma directive then by default the compiler ignores it. So we can say that the pragma directive is very useful for managing errors.

I hope this article will be useful for all of you to mange the warnings. 

Up Next
    Ebook Download
    View all
    Learn
    View all