How to suppress issues in C# code

I like to maintain a zero-warning policy in a project. In the case of false-alarm warnings, I think it’s better to suppress them than to leave them active. When we suppress a warning, it will not draw our attention every time we look at logs.

So, how can we suppress a warning like the example one below?

An example of a false-alarm warning that we might want to suppress

Option 1: pragma directives

Visual Studio suggests that method by default. I find it ugly because it decreases the readability of the code. Especially that in the default code style rules, the code has no indentation before the pragma directive.

#pragma warning disable S2699 // Tests should include assertions
    public void WhenMyClassIsInstantiatedExpectNoException()
#pragma warning restore S2699 // Tests should include assertions
        // Arrange, Act
        new MyClass();

        // Assertion is implicit. No exceptions means that the test passed.
Code language: C# (cs)

Option 2: the SuppressMessage attribute

I believe that the System.Diagnostics.CodeAnalysis.SuppressMessage attribute leads to much more elegant code. It doesn’t decrease readability that much when we scan the code with our eyes:

    [SuppressMessage("Blocker Code Smell", "S2699:Tests should include assertions",
        Justification = "Assertion is implicit. No exceptions means that the test passed.")]
    public void WhenMyClassIsInstantiatedExpectNoException()
        new MyClass();
Code language: C# (cs)

The only piece of that attribute which is really required is the second argument, with the code of a suppressed warning. This abbreviated form would technically work equally well:

 [SuppressMessage("Sonar", "S2699")]Code language: C# (cs)

Option 3: exclusions in *.csproj

If we want to globally disable some type of warning in the scope of the entire project, the *.csproj file is a good place to do so. We can specify multiple warnings, separating them with a comma or a semicolon.

<Project Sdk="Microsoft.NET.Sdk">


  <!-- ... the rest of the file -->
Code language: HTML, XML (xml)

Option 4: the .editorconfig file

The severity of the warnings, including the ones from the custom code analyzers like SonarAnalyzer, can also be configured in the .editorconfig file. Visual Studio 2022 even has a nice UI for that:

Under the hood it’s still just a plain text file, and the setting translates to the following fragment in the .editorconfig file:

dotnet_diagnostic.S2699.severity = none

Option 5: a suppression file

A list of exceptions can also be centrally managed in a suppression file. I don’t see many advantages to this method. From my experience, in the projects it often grows into a pile of unmaintained rubbish. There is no natural incentive to ever delete anything from this file. It’s usually generated by the IDE and has some custom conventions for Target that we might not understand at first glance.

Have I missed any other option worth mentioning? 😉

Leave a Comment