Click here to Skip to main content
13,287,658 members (47,551 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 27 Aug 2010

How to suppress compiler warning using #pragma warning directives in Visual Studio

, 27 Aug 2010
Rate this:
Please Sign up or sign in to vote.
A post on how to suppress compiler warning using #pragma warning directives in Visual Studio

Sometimes during development, you may need to avoid the unnecessary compiler warnings to show in warning window. Though it’s not recommended to suppress the warning explicitly, but during some debugging and analysis of code, you may want to hide them for the time being. In this post, I have explained how you can hide the unwanted warning explicitly by specifying the warning number.

To suppress the warning, you need to use #pragma warning directives in your code. Below is the syntax for the same.

#pragma warning disable <warning-Numbers>

[Line of Code ]

Let’s consider you are having the below code block and you want to suppress the warning messages.


If you are going to build the above code, you will find the below warning messages:


Now, you can use #pragma warning directives to disable those compilation warnings. You have to mention the warning number along with the directives. If you want to suppress multiple warnings, you have to use the comma separated warning number.


This addition of warning disable directives will remove all the specified compilation warning.


To suppress warnings, you have to mention the warning number. So, you have to make sure you are providing the correct warning number. You can find out the warning number from the output window while building the application.


Similarly you can use #pragma warning restore <warning-lists> to restore the compilation warning message.

note If there is no warning number provided with the #pragma warning directives, it will suppress all the warnings.


In this blog post, I have explained how we can explicitly suppress the compilation warning message using #pragma directives. You can disable a single warning or multiple warnings with a single statement by giving warning numbers with comma (,) separated. I have also shown how to find out the correct warning number from the output window.

Hope this will help you!

Filed under: General, Tips and Tricks, Visual Studio


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Abhijit Jana
Technical Lead
India India
.NET Consultant | Former Microsoft MVP - ASP.NET | CodeProject MVP, Mentor, Insiders| Technology Evangelist | Author | Speaker | Geek | Blogger | Husband

Blog :
Web Site :
Twitter : @AbhijitJana
My Kinect Book : Kinect for Windows SDK Programming Guide

You may also be interested in...


Comments and Discussions

GeneralReally Nice. Pin
Abhishek Sur29-Aug-10 0:17
mvpAbhishek Sur29-Aug-10 0:17 
GeneralSingle File Pin
Silic0re0927-Aug-10 13:21
memberSilic0re0927-Aug-10 13:21 
GeneralRe: Single File Pin
Abhijit Jana27-Aug-10 20:07
mvpAbhijit Jana27-Aug-10 20:07 
GeneralMy vote of 5 Pin
KunalChowdhury27-Aug-10 8:59
mentorKunalChowdhury27-Aug-10 8:59 
AnswerRe: My vote of 5 Pin
Abhijit Jana27-Aug-10 9:06
mvpAbhijit Jana27-Aug-10 9:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 27 Aug 2010
Article Copyright 2010 by Abhijit Jana
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid