-
Notifications
You must be signed in to change notification settings - Fork 6k
Ca1846 #24325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Ca1846 #24325
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bc1b25c
Add ca1846.md
NewellClark 72f740a
Update ca1846.md
NewellClark 968d75f
Add CA1846
NewellClark 0208363
Add CA1846
NewellClark 83b065c
Add CA1846
NewellClark 75958e7
Remove trailing spaces
NewellClark 0ee0d06
Apply suggestions from code review
NewellClark 1668fd8
Add VB example
NewellClark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
--- | ||
title: "CA1846: Prefer `AsSpan` over `Substring`" | ||
description: "Learn about code analysis rule CA1846: Prefer `AsSpan` over `Substring`" | ||
ms.date: 05/21/2021 | ||
ms.topic: reference | ||
f1_keywords: | ||
- CA1846 | ||
- PreferAsSpanOverSubstring | ||
helpviewer_keywords: | ||
- PreferAsSpanOverSubstring | ||
- CA1846 | ||
author: NewellClark | ||
dev_langs: | ||
- CSharp | ||
- VB | ||
--- | ||
# CA1846: Prefer `AsSpan` over `Substring` | ||
|
||
| | Value | | ||
|-|-| | ||
| **Rule ID** |CA1846| | ||
| **Category** |[Performance](performance-warnings.md)| | ||
| **Fix is breaking or non-breaking** |Non-breaking| | ||
|
||
## Cause | ||
|
||
The result of a call to one of the <xref:System.String.Substring%2A?displayProperty=nameWithType> 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 <xref:System.Span%601> and <xref:System.ReadOnlySpan%601> 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 <xref:System.MemoryExtensions.AsSpan%2A?displayProperty=nameWithType> extension methods. | ||
|
||
```csharp | ||
gewarren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
|
||
- [Performance warnings](performance-warnings.md) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.