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:
- #pragma warning disable <warning code/warning name>
- {
-
- }
If we want to restore any warnings in our code then we use the following syntax:
- #pragma warning restore <warning code/warning name>
- {
-
- }
Here is an example in which we have warnings.
Code
- using System;
- class warning
- {
- public static void Main()
- {
- string name;
-
- while (false)
-
-
- {
- int value = 5;
-
- if (value == null)
- {
-
- }
-
- }
- Console.WriteLine("hi rizwan...!!!!!");
- Console.Read();
-
- }
- }
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
- using System;
-
- class warning
- {
- public static void Main()
- {
- #pragma warning disable CS0168
- string name;
-
- while (false)
- {
- #pragma warning disable CS0162
- #pragma warning disable CS0472
- int value = 5;
- if (value == null)
- {
-
- }
-
- }
- Console.WriteLine("hi rizwan...!!!!!");
- Console.Read();
-
- }
- }
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
- using System;
-
- class warning
- {
- public static void Main()
- {
- #pragma warning disable CS0168
- string name;
-
- while (false)
- {
- #pragma warning restore CS0162 // by help of this we show the warnings in output window
- #pragma warning disable CS0472
- int value = 5;
- if (value == null)
- {
-
- }
-
- }
- Console.WriteLine("hi rizwan...!!!!!");
- Console.Read();
-
- }
- }
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.