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:
bool,byte, andsbyteare supported whenever System.ReadOnlySpan is available.short,ushort,char,int,uint,float,long,ulong, anddoubleare supported only when the target compilation exposes the public static [System.Runtime.CompilerServices.RuntimeHelpers.CreateSpan`(System.RuntimeFieldHandle)](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.runtimehelpers.createspan--1) method. .NET 7 and later reference assemblies expose this method.
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
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
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.