John Smith
John Smith

Reputation: 715

How do you disable CA1814 with #pragma warning?

My background: I have a file with a large number of lines that cause this warning (where this specific warning is meaningless to me because the array sizes are identical).

To rule out some obscure project settings, I created a simple toy example. Here, I use the rule set "Microsoft All Rules". The following still gives me warnings, including CA1814:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
#pragma warning disable 1814 
            var test = new int[0,0,0];
            var b = new Banana();
#pragma warning restore 1814 
            return;
        }
    }

    class Banana : IDisposable
    {
        public void Dispose()
        {
            Console.WriteLine("What a waste");
        }
    }
}

Warnings

I'm not sure if this is somehow related, but even with all these errors, I can build the project fine using Warning level: 4, Treat warnings as errors: "All".

edit: I also tried CA1814 instead of just 1814

Upvotes: 1

Views: 1419

Answers (1)

Matt Dillard
Matt Dillard

Reputation: 14853

CS1814 is not a compiler warning, per se; it is a Code Analysis warning. You have enabled static code analysis on your project, and have opted into all of the rules. You may not care to follow all of them, though, and that's what you're running up against.

You probably want to customize your ruleset. You can do that in one of two ways:

  1. Selectively disable a warning using the SuppressMessage attribute
  2. Create a new set of rules based on the "Microsoft All Rules" set, and configure it not to include CS1814.

For the first approach, add the following attribute to the top of the file or method:

[SuppressMessage("Microsoft.Performance", "CA1814:PreferJaggedArraysOverMultidimensional")]

Upvotes: 1

Related Questions