With C# 11, a new feature that has not been advertised arrives. Pattern matching for ReadOnly/Span on constant string is now available. Let’s see an example:

var text = "hello".AsSpan();

switch (text)
{
    case "hello":
        Console.WriteLine("hi"); 
        break;
    default:
        break;
}
//output: "hi"

Keep in mind that you can already use equality check with a ReadOnly/Span.

Leave a comment

Trending