[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Disable Visual Studio warnings in C#

[Disable Visual Studio warnings in C#]

Visual Studio warnings let you know when your code contains something suspicious. For example, suppose you have XML documentation enabled.

To enable XML documentation, open the Project menu and select Properties. On the Build tab, check the XML Documentation File box and enter a file to hold the documentation. On the same tab, set the Warning Level to 4. (If that value is lower, Visual Studio does not display missing XML documentation warnings.)

If you enable XML documentation like this, Visual Studio warnings let you know if any public class members are missing XML documentation. The idea is that public members should have documentation to provide IntelliSense support. (And possibly text in documents that you may want to generate.) Visual Studio displays the following warning if the Form1 class defines a public method named Method3 without XML comments.

Missing XML comment for publicly visible type or member 'howto_disable_warnings.Form1.Method3()'

The picture at the top of this post shows this error in the Error window.

However, sometimes Visual Studio warnings are unnecessary. For example, if you give a class a public method but don't want to give it XML comments, you don't really want to see the warning all the time.

In general, it's not a good idea to leave Visual Studio warnings in the Error List. If the Error List is full of warnings that you want to ignore, the you won't be able to see the ones you don't want to ignore. Fortunately, you can use a pragma directive to remove specific Visual Studio warnings if you like.

First, you need to find out the warning number. In some versions of Visual Studio, you can find the number by digging through the Output window. In all versions (as far as I know), you can click on the warning in the Error List and then press F1.

You can also find a list of error and warning codes on the page C# Compiler Errors. Unfortunately, they're listed by number not name.

Once you know the warning's number, you can disable it and re-enable it as needed. The following code shows an example.

public partial class Form1 : Form { /// <summary> /// Constructor. /// </summary> public Form1() { InitializeComponent(); } /// <summary> /// This method does something that should /// be documented with an XML comment. /// </summary> public void Method1() { } // This method doesn't need XML documentation. #pragma warning disable 1591 public void Method2() #pragma warning restore 1591 { } // This method doesn't have XML documentation but should. public void Method3() { } }

The code's Form1 constructor and methods are public, so they all need XML comments. The code includes XML comments for the constructor and Method1.

The statement #pragma warning disable 1591 disables warning number 1591, which is the one you get if a method is missing an XML comment. When the compiler processes Method2, it doesn't add this warning to the Error List.

The statement #pragma warning restore 1591 re-enables warning number 1591. When the compiler processes Method3, it displays this warning in the Error List.

Warnings are there for a reason, so you shouldn't just disable every warning you see. Instead fix whatever it is that's causing the warning. If you really can't fix the issue and you know it won't be a problem, then you can use pragma to disable the warning for a specific piece of code.

To avoid missing other warnings, only disable the warning for the smallest piece of code that makes sense.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.