Properties should not return arrays
Properties should not return arrays
Microsoft docsDescription
Arrays returned by properties are not write-protected, even if the property is read-only. To keep the array tamper-proof, the property must return a copy of the array. Typically, users won't understand the adverse performance implications of calling such a property. Specifically, they might use the property as an indexed property.
Cause
A property returns an array.
By default, this rule only looks at externally visible properties and types, but this is configurable.
How to fix violations
To fix a violation of this rule, either make the property a method or change the property to return a collection.
Example
#pragma warning disable CA1819
// The code that's violating the rule is on this line.
#pragma warning restore CA1819When to suppress
You can suppress a warning that's raised for a property of an attribute that's derived from the System.Attribute class. Attributes can contain properties that return arrays, but can't contain properties that return collections.
You can suppress the warning if the property is part of a Data Transfer Object (DTO)) class.
Otherwise, do not suppress a warning from this rule.