C#idiomatic C#
ca1878fundamentals/code-analysis/quality-rules

Code analysis

CA1878: Prefer ReadOnlySpan properties over readonly array fields

Learn about code analysis rule CA1878: Prefer ReadOnlySpan properties over readonly array fields

CA1878 Performance enabled by default in .NET 11: As suggestion
Fix is breaking or non-breaking Non-breakingApplicable languages C#

Cause

A private static readonly one-dimensional array field contains supported constant values, and all observed usages can be safely rewritten to use a System.ReadOnlySpan property.

Rule description

A static readonly array field requires a managed array allocation. When a System.ReadOnlySpan property returns supported constant data, the C# compiler can lower the data without allocating a managed array. Replacing the field with a property can reduce allocations while preserving read-only access to the data.

The analyzer supports the following element types:

The analyzer doesn't diagnose arrays of decimal, nint, nuint, enum types, arbitrary structs, or reference types. It also doesn't diagnose jagged arrays or multidimensional arrays.

How to fix violations

Replace the array field with an expression-bodied property that returns System.ReadOnlySpan.

The code fix rewrites eligible usages of the field. For supported AsSpan calls, it removes a redundant call or replaces the call with an equivalent span operation, such as Slice. The code fix isn't offered when it can't preserve the semantics of every usage.

Example

The following code snippet shows a violation of CA1878:

sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1878.csViolation

csharp
using System;

// <Violation>
class CA1878ViolationExample
{
    private static readonly byte[] Prefix = new byte[] { 0x50, 0x4B, 0x03, 0x04 };

    public static ReadOnlySpan<byte> GetPrefix() => Prefix;
}
// </Violation>

// <Fix>
class CA1878FixExample
{
    private static ReadOnlySpan<byte> Prefix => new byte[] { 0x50, 0x4B, 0x03, 0x04 };

    public static ReadOnlySpan<byte> GetPrefix() => Prefix;
}
// </Fix>

The following code snippet fixes the violation:

sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1878.csFix

csharp
using System;

// <Violation>
class CA1878ViolationExample
{
    private static readonly byte[] Prefix = new byte[] { 0x50, 0x4B, 0x03, 0x04 };

    public static ReadOnlySpan<byte> GetPrefix() => Prefix;
}
// </Violation>

// <Fix>
class CA1878FixExample
{
    private static ReadOnlySpan<byte> Prefix => new byte[] { 0x50, 0x4B, 0x03, 0x04 };

    public static ReadOnlySpan<byte> GetPrefix() => Prefix;
}
// </Fix>

When to suppress warnings

Suppress a warning if you intentionally retain the array's identity or mutability, or if preserving API compatibility requires the member to remain an array field. The rule only reports private static readonly fields whose observed usages can be safely rewritten.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

0

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

1

For more information, see How to suppress code analysis warnings.

See also