1

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

John Smith
  • 704
  • 7
  • 19

1 Answers1

1

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")]
Matt Dillard
  • 14,677
  • 7
  • 51
  • 61
  • I'm currently doing 1. but it looks ridiculous due to the number of lines. For 2., we have too many people working on the project (and I think no one wants to set a precedent for a ruleset modification, including myself). Therefore, I was hoping for a third way. – John Smith Oct 24 '18 at 13:57
  • I understand not wanting to be the first to make a ruleset modification, but in my experience with rulesets, you almost always want to customize them in some way. There's a huge kitchen sink full of rules, and I don't think it's a black mark at all to decide that some are not relevant for your project. Going with a new custom ruleset means that everyone can share it, and you won't have to suppress the same messages over and over again. – Matt Dillard Oct 24 '18 at 14:00
  • Also, I don't follow some of what you're saying - perhaps you can clear it up. You say you're currently following approach #1. In that case, why are you seeking to use `#pragma` directives? Are those not just as verbose as the suppression attributes? – Matt Dillard Oct 24 '18 at 14:03
  • As I understand, I could disable the warning for an entire section of code using #pragma whereas with suppression I need to add an SuppressMessage attribute to each offending line. I'd be fine with using attributes if they allowed for controlled suppression for some region. – John Smith Oct 24 '18 at 14:06
  • I see. If you'll following the link I posted about the SuppressMessage attribute, you'll see how it can target a given line, a method, a class, or even the whole project. – Matt Dillard Oct 24 '18 at 14:08
  • According to the first answer in: https://stackoverflow.com/questions/32357046/how-to-suppress-code-analysis-messages-for-all-type-members?rq=1, blanket suppressions for an entire class is not possible. I already tried SuppressMessage with Scope=type with no luck. Could you elaborate a bit? To clarify my situation a bit further, I want to suppress the message both in members as well as in method signatures in a single specific class. – John Smith Oct 24 '18 at 15:29
  • You may not be able to scope this particular rule as much as you'd like. The CA rules differ in whether they respect the scopes provided. It's a matter of how an individual rule was implemented. – Matt Dillard Oct 25 '18 at 13:17