Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

CA1870: Use a cached 'SearchValues' instance

Feedback

In this article

PropertyValue
Rule IDCA1870
TitleUse a cached 'SearchValues' instance
CategoryPerformance
Fix is breaking or non-breakingNon-breaking
Enabled by default in .NET 10As suggestion

Cause

AnIndexOfAny orContainsAny method is called with many constant values in a way that can benefit from usingSearchValues instead.

The rule doesn't flag calls that use up to five values, as those already use an optimal implementation.

Rule description

Using a cachedSearchValues<T> instance is more efficient than passing values toIndexOfAny orContainsAny directly.

How to fix violations

Create and cache aSearchValues<T> instance in astatic readonly field, then pass that instance to theIndexOfAny orContainsAny call instead.

Acode fix that automatically performs this transformation is available.

Example

The following code snippet shows two violations of CA1870:

static readonly char[] MyValues = new[] { 'a', 'b', 'c', 'x', 'y', 'z' };static int IndexOfMyValues(ReadOnlySpan<char> text){    return text.IndexOfAny(MyValues);}static bool ContainsOnlyMyValues(ReadOnlySpan<char> text){    return !text.ContainsAnyExcept("abcxyz");}

The following code snippet fixes the violations:

private static readonly SearchValues<char> s_myValues = SearchValues.Create("abcxyz");static int IndexOfMyValues(ReadOnlySpan<char> text){    return text.IndexOfAny(s_myValues);}static bool ContainsOnlyMyValues(ReadOnlySpan<char> text){    return !text.ContainsAnyExcept(s_myValues);}

If there are multiple calls toIndexOfAny with the same set of values,s_myValues should be reused.

When to suppress warnings

It's safe to suppress this warning if performance isn't a concern.

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.

#pragma warning disable CA1870// The code that's violating the rule is on this line.#pragma warning restore CA1870

To disable the rule for a file, folder, or project, set its severity tonone in theconfiguration file.

[*.{cs,vb}]dotnet_diagnostic.CA1870.severity = none

For more information, seeHow to suppress code analysis warnings.

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?