Prefer AsSpan over Substring
Prefer AsSpan over Substring
Microsoft docsDescription
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.Span1 and System.ReadOnlySpan1 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.
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>.
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.
Example
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);
}When to suppress
It is safe to suppress warnings from this rule if performance is not a concern.