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

Code analysis

CA1846: Prefer `AsSpan` over `Substring`

Learn about code analysis rule CA1846: Prefer AsSpan over Substring

CA1846 Performance enabled by default in .NET 10: As suggestion
Fix is breaking or non-breaking Non-breakingApplicable languages C# and Visual Basic

Cause

The result of a call to one of the System.String.Substring overloads is passed to a method with an available overload that accepts ReadOnlySpan<Char>.

Rule description

Substring allocates a new string object on the heap and performs a full copy of the extracted text. String manipulation is a performance bottleneck for many programs. Allocating many small, short-lived strings on a hot path can create enough collection pressure to impact performance. The O(n) copies created by Substring become relevant when the substrings get large. The System.Span and System.ReadOnlySpan types were created to solve these performance problems.

Many APIs that accept strings also have overloads that accept a ReadOnlySpan<System.Char> argument. When such overloads are available, you can improve performance by calling AsSpan instead of Substring.

How to fix violations

To fix a violation of this rule, replace the call to string.Substring with a call to one of the System.MemoryExtensions.AsSpan extension methods.

csharp
using System;

public void MyMethod(string iniFileLine)
{
    // Violation
    int.TryParse(iniFileLine.Substring(7), out int x);
    int.TryParse(iniFileLine.Substring(2, 5), out int y);

    // Fix
    int.TryParse(iniFileLine.AsSpan(7), out int x);
    int.TryParse(iniFileLine.AsSpan(2, 5), out int y);
}
vb
Imports System

Public Sub MyMethod(iniFileLine As String)
    Dim x As Integer
    Dim y As Integer

    ' Violation
    Integer.TryParse(iniFileLine.Substring(7), x)
    Integer.TryParse(iniFileLine.Substring(2, 5), y)

    ' Fix
    Integer.TryParse(iniFileLine.AsSpan(7), x)
    Integer.TryParse(iniFileLine.AsSpan(2, 5), y)
End Sub

When to suppress warnings

It is safe to suppress warnings from this rule if performance is not a concern.

See also