About
The series of features exploring in .NET 9 posts, This post, we are focusing on "SearchValues" improvements did in .NET9. Please refer the following links for other .NET 9 features discussed in my earlier posts.
SearchValues was introduced in .NET8 to perform searching more efficiently compared to earlier method of using ICollection.Contains method. However, in .NET8, Its limited to set of characters or bytes.
So, .NET9 its upgraded to support string as well instead of only character.
Lets explore the usage of "SearchValues" usage in both .NET8 and .NET9 versions.
The below code example shows the usage of this feature in both .NET versions.
We can see that, .NET9 search supports string comparison type as well.
public static void SearchValuesFeature()
{
var message = "Explore new feature of SearchValues improvements in .NET9".AsSpan();
// .NET 8
var charSearch = SearchValues.Create(['.','N', 'E', 'T']);
Console.WriteLine(message.ContainsAny(charSearch));
// .NET 9
var wordSearch = SearchValues.Create([".NET9", "of"], StringComparison.OrdinalIgnoreCase);
Console.WriteLine(message.ContainsAny(wordSearch));
}
This feature is defiantly used option in our day to day projects/development where we used to do search operations, data input filter scenarios, spam detection checks and many more.
Thanks for your reading!!
No comments:
Post a Comment