All rules
CA1819Performance Enabled by default: No

Properties should not return arrays

Properties should not return arrays

Microsoft docs

Description

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 CA1819

When 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.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0