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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...