TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Pragma Directive

This C# example program uses the #pragma directive. This directive affects the reporting of warnings.

Pragma directive. The #pragma directive disables the reporting of a warning.

It is useful when you understand and expect the warning—but still want to disable it. With #pragma we can disable a specific warning or all warnings.

Example. As we begin, please remember that when the C# compiler detects unreachable code, it will report a warning. The compiler reports these errors to improve code quality. In this example, the if (false) statement results in unreachable code.

Unreachable Code Detected

Tip: By wrapping the #pragma warning disable directive and #pragma warning restore directive around the statement, we can hide the warning.

So: When you compile this program, no warnings are reported by the C# compiler.

Compiler

C# program that uses pragma directive

using System;

class Program
{
    static void Main()
    {
	// This example has unreachable code!
	// ... The pragma directives hide the warning.
#pragma warning disable
	if (false)
	{
	    Console.WriteLine("Perls");
	}
#pragma warning restore
    }
}

Result
    When compiled, no warnings are issued.

Specifying specific warnings. You can optionally add another value after the directives. As the C# specification shows, you can use #pragma warning disable 612 to disable the C# compiler's warning number 612.

Tip: This is probably more trouble than it is worth. It might be better to just disable all warnings in small blocks of code.

Discussion. I think the #pragma warning disable and restore directives are useful in many programs. When developing, I sometimes will use the if (false) construction to comment out code but compile it anyways.

This ensures that the code will not stop compiling and refactoring will update it. I can use the #pragma directives to indicate that I know the code is unreachable already and don't want to fix it.

Summary. The #pragma warning disable and restore directives give you the ability to influence how the compiler reports warnings. If you expect a certain warning to occur, and don't want to fix it, #pragma directives are useful.

Compile-Time Error


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf