1

In my C# code I sometimes use a #pragma to suppress compiler warnings. For example without the pragma this code would give a compiler warning:

#pragma warning disable CS1998
    public virtual async Task Initialize()
    {
        StartLogging(true, true);
    }
#pragma warning restore CS1998

I seem to remember that there is a way to do this by decorating the method with an attribute instead of using the #pragma but I cannot find the documentation. How is it done?

dumbledad
  • 16,305
  • 23
  • 120
  • 273

1 Answers1

1

In his answer to another question Fábio shows the technique:

[System.Diagnostics.CodeAnalysis.SuppressMessage("Await.Warning", "CS4014:Await.Warning")]
static async Task StartWorkAsync()
{
    WorkAsync();
}

He goes on to note that

The important part of this code is the second parameter. The "CS4014:" part is what suppresses the warning. You can write anything you want on the rest.

The documentation for the SuppressMessageAttribute class is available here and there is an overview of in source suppression here

dumbledad
  • 16,305
  • 23
  • 120
  • 273