|
| 1 | +--- |
| 2 | +title: "CA1870: Use a cached 'SearchValues' instance" |
| 3 | +description: "Learn about code analyzer rule CA1870 - Use a cached 'SearchValues' instance" |
| 4 | +ms.date: 09/12/2023 |
| 5 | +ms.topic: reference |
| 6 | +f1_keywords: |
| 7 | + - CA1870 |
| 8 | + - UseSearchValuesAnalyzer |
| 9 | +helpviewer_keywords: |
| 10 | + - CA1870 |
| 11 | +dev_langs: |
| 12 | + - CSharp |
| 13 | +author: MihaZupan |
| 14 | +ms.author: mizupan |
| 15 | +--- |
| 16 | + |
| 17 | +# CA1870: Use a cached 'SearchValues' instance |
| 18 | + |
| 19 | +| Property | Value | |
| 20 | +|-------------------------------------|----------------------------------------| |
| 21 | +| **Rule ID** | CA1870 | |
| 22 | +| **Title** | Use a cached 'SearchValues' instance | |
| 23 | +| **Category** | [Performance](performance-warnings.md) | |
| 24 | +| **Fix is breaking or non-breaking** | Non-breaking | |
| 25 | +| **Enabled by default in .NET 8** | Yes | |
| 26 | + |
| 27 | +## Cause |
| 28 | + |
| 29 | +An `IndexOfAny` or `ContainsAny` method is called with many constant values in a way that can benefit from using <xref:System.Buffers.SearchValues> instead. |
| 30 | + |
| 31 | +The rule doesn't flag calls that use up to five values, as those already use an optimal implementation. |
| 32 | + |
| 33 | +## Rule description |
| 34 | + |
| 35 | +Using a cached <xref:System.Buffers.SearchValues%601> instance is more efficient than passing values to `IndexOfAny` or `ContainsAny` directly. |
| 36 | + |
| 37 | +## How to fix violations |
| 38 | + |
| 39 | +Create and cache a <xref:System.Buffers.SearchValues%601> instance in a `static readonly` field, then pass that instance to the `IndexOfAny` or `ContainsAny` call instead. |
| 40 | + |
| 41 | +A code fixer that performs this transformation is available. |
| 42 | + |
| 43 | +## Example |
| 44 | + |
| 45 | +The following code snippet shows two violations of CA1870: |
| 46 | + |
| 47 | +```csharp |
| 48 | +static readonly char[] MyValues = new[] { 'a', 'b', 'c', 'x', 'y', 'z' }; |
| 49 | + |
| 50 | +static int IndexOfMyValues(ReadOnlySpan<char> text) |
| 51 | +{ |
| 52 | + return text.IndexOfAny(MyValues); |
| 53 | +} |
| 54 | + |
| 55 | +static bool ContainsOnlyMyValues(ReadOnlySpan<char> text) |
| 56 | +{ |
| 57 | + return !text.ContainsAnyExcept("abcxyz"); |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +The following code snippet fixes the violations: |
| 62 | + |
| 63 | +```csharp |
| 64 | +private static readonly SearchValues<char> s_myValues = SearchValues.Create("abcxyz"); |
| 65 | + |
| 66 | +static int IndexOfMyValues(ReadOnlySpan<char> text) |
| 67 | +{ |
| 68 | + return text.IndexOfAny(s_myValues); |
| 69 | +} |
| 70 | + |
| 71 | +static bool ContainsOnlyMyValues(ReadOnlySpan<char> text) |
| 72 | +{ |
| 73 | + return !text.ContainsAnyExcept(s_myValues); |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +If there are multiple calls to `IndexOfAny` with the same set of values, `s_myValues` should be reused. |
| 78 | + |
| 79 | +## When to suppress warnings |
| 80 | + |
| 81 | +It's safe to suppress this warning if performance isn't a concern. |
| 82 | + |
| 83 | +## Suppress a warning |
| 84 | + |
| 85 | +If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. |
| 86 | + |
| 87 | +```csharp |
| 88 | +#pragma warning disable CA1870 |
| 89 | +// The code that's violating the rule is on this line. |
| 90 | +#pragma warning restore CA1870 |
| 91 | +``` |
| 92 | + |
| 93 | +To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). |
| 94 | + |
| 95 | +```ini |
| 96 | +[*.{cs,vb}] |
| 97 | +dotnet_diagnostic.CA1870.severity = none |
| 98 | +``` |
| 99 | + |
| 100 | +For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). |
0 commit comments