Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions docs/standard/base-types/string-comparison-net-5-plus.md
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps add a link to this section from the second bullet point in https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8/runtime#performance-focused-types.

Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,38 @@ ReadOnlySpan<char> span = s.AsSpan();
if (span.StartsWith("Hello", StringComparison.Ordinal)) { /* do something */ } // ordinal comparison
```

## Efficient multi-value string comparisons

When comparing a string against a fixed set of known values repeatedly, consider using `SearchValues<T>` instead of chained comparisons or LINQ-based approaches.
`SearchValues<T>` can precompute internal lookup structures and optimize the comparison logic based on the provided values.

### Recommended usage

Create and cache the `SearchValues<string>` instance once, then reuse it for comparisons:

```cs
using System.Buffers;

private static readonly SearchValues<string> Commands = SearchValues.Create(new[] { "start", "run", "go" }, StringComparison.OrdinalIgnoreCase);

if (Commands.Contains(command))
{
/* do something */
}
```
### Avoid repeated creation
Avoid creating `SearchValues` instances inside hot paths or per comparison, as the creation step can be relatively expensive:

```cs
// Avoid this pattern
if (SearchValues.Create(new[] { "start", "run", "go" }, StringComparison.OrdinalIgnoreCase).Contains(command))
{
/* do something */
}
```
Caching and reusing the instance ensures optimal performance.


## See also

- [Globalization breaking changes in .NET 5](../../core/compatibility/5.0.md#globalization)
Expand Down