While browsing through the list of changes in .NET 9, I noticed a remark about the SearchValues functionality. I had no idea what it does so time to further investigate this feature...
The SearchValues<T>
type was introduced in .NET 8 to help optimize search. It allows us to specify the values we want to search for. Based on the provided values the runtime will come up with different strategies to optimize the search performance.
After the SearchValues<T>
is created, we can use it on any ReadOnlySpan<T>
.
Here is a small example:
In .NET 8, the SearchValues was limited to simple data types like char. Starting from .NET 9, you can also use string values:
Remark: Use SearchValues<T>
when you expect to use the same search values a lot.
More information
SearchValues Class (System.Buffers) | Microsoft Learn