When comparing spans, using the equality/inequality operator will compare the addresses of the objects pointed to by the spans, not the contents. If you do not want to compare the addresses directly, you should use pattern matching or SequenceEqual method.
When comparing Span and a constant, you should use pattern matching instead of the equality/inequality operators, unless you want to compare what the addresses of the objects pointed to.
When comparing Span and a non-constant, you should use SequenceEqual method instead of the equality/inequality operators, unless you want to directly compare what the addresses of the objects pointed to.
public class TestClass {
public void TestMethod() {
var span = "TEST".AsSpan(0, 2);
var compare1 = span == "TE";
var chr = 'E';
var compare2 = span == [ 'T', chr ];
var ca = [ 'T', 'E' ];
var compare3 = span == ca;
}
}
public class TestClass {
public void TestMethod() {
var span = "TEST".AsSpan(0, 2);
var compare1 = span is "TE";
var chr = 'E';
var compare2 = span.SequenceEqual([ 'T', chr ]);
var ca = [ 'T', 'E' ];
var compare3 = span.SequenceEqual(ca);
}
}