C# Tips and Tricks #8 – Disable specific compiler warnings in Visual Studio

Although not a good practise , cometime you might want to hide a specific warning of your C# code.

For example , assume that you have an integer declared in your code and is never used as shown below.

using System;
namespace ConsoleApp1
{
    class Program
    {   
        static void Main(string[] args)
        {
            int variable1 = 10;
            Console.ReadLine();
        }
    }
}

When you build the above code , Visual Studio will show the compiler warnings in the Output Window and the Error List.

Warning CS0219 The variable ‘variable1’ is assigned but its value is never used

image

If you are sure what you are doing and really want to hide this warning , you can easily do it using the pragma warning directive. Just place the pragma warning directive above the code for which you want to hide the warning as shown below.

Place the directive immediately above the offending line and reference warning #219.

image

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...