C#

C# Pragma, The Inline warning control In C# | Compiler Directives C#

Pragma is a compiler directive in C#. This article explains on #pragma directives in C# and the various ways you can control the code analysis warnings that we get during the compilation of your .NET code.
When we develop C# code, the compiler occasionally issues warnings. Warnings are generally ignored by programmers because they do not prohibit the application from running. If our software contains any warnings, however, the warnings have no effect on the code. The #pragma directive is a feature of C# to remove the warnings.
When we declare a variable but do not use it elsewhere in the program, the compiler issues a warning “Variable is declared, it is never used”. We can eliminate all these kinds of warnings by using the #pragma.

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

Similarly, you can enable warnings
   #pragma warning enable 3021

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.

Rajeev

Recent Posts

OWIN Authentication in .NET Core

OWIN (Open Web Interface for .NET) is an interface between web servers and web applications…

2 years ago

Serializing and Deserializing JSON using Jsonconvertor in C#

JSON (JavaScript Object Notation) is a commonly used data exchange format that facilitates data exchange…

2 years ago

What is CAP Theorem? | What is Brewer’s Theorem?

The CAP theorem is also known as Brewer's theorem. What is CAP Theorem? CAP theorem…

2 years ago

SOLID -Basic Software Design Principles

Some of the Key factors that need to consider while architecting or designing a software…

2 years ago

What is Interface Segregation Principle (ISP) in SOLID Design Principles?

The Interface Segregation Principle (ISP) is one of the SOLID principles of object-oriented design. The…

2 years ago

What is Single Responsibility Principle (SRP) in SOLID Design Priciples?

The Single Responsibility Principle (SRP), also known as the Singularity Principle, is a software design…

2 years ago