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
Pin It

Leave a Reply

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