Skip to content

CA1861 reporting for inline arrays in constructors of static properties #7365

Open
@johnthcall

Description

Description

When setting a property outside of a constructor in a static properties setup it will report CA1861, if the same array is set via a constructor the analyzer will not report. It's possible that when the array is set via a constructor parameter an optimization is made by the compiler that it can't make by setting the property "after" the constructor.

Reproduction Steps

The following will report CA1861 on the array creation.

        public class TestClass
        {
            public List<IList<string>> ListEntry
            {
                get; set;
            }
        }

        public static TestClass TestClass1 = new TestClass { ListEntry = new List<IList<string>> { new[] { "test" } } };

If the array is a constructor parameter it will not report like in the following

public class TestClass
 {
  public TestClass(List<IList<string>> ListEntry) { }
}

public static TestClass TestClass1 = new TestClass(new List<IList<string>> { new[] { "test" } });

Expected behavior

As this is a static property and the category is performance I believe it should not fire as it is only allocating the array once.

Actual behavior

The analyzer should not detect an issue

Regression?

No response

Known Workarounds

Add the initialization to a constructor or invoke the auto refactor in VS to make a cosnt array.

Configuration

Windows 11 x64 on .Net 8 RC2 (8.0.0-rc.2.23479.6)

Other information

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions