0

I have come across this bizarre behavior where two equivalent IReadOnlySpan<Char> are failing equality check, debugging seems to show that both are identical yet operator== returns false

ReadOnlySpan<Char> s1 = "true false"[0..4];
ReadOnlySpan<Char> s2 = "true";

Console.WriteLine(s1 == s2);
Ricky Spanish
  • 705
  • 6
  • 16
  • 4
    [Msdn ReadOnlySpan.Equality Operator](https://learn.microsoft.com/en-us/dotnet/api/system.readonlyspan-1.op_equality?view=net-5.0#remarks) : In the remark session _"This tests if two ReadOnlySpan instances point to the same starting memory location, and have the same Length values. This does not compare the contents of two ReadOnlySpan instances."_ – Drag and Drop Nov 23 '22 at 12:26
  • @DragandDrop aha, I totally missed that, is there a built-in way to compare the contents without converting it to string? – Ricky Spanish Nov 23 '22 at 12:27
  • I will say sequence equals maybe : https://stackoverflow.com/questions/51010205/best-way-to-compare-readonlymemoryt-instances , Or [MemoryExtensions Class](https://learn.microsoft.com/en-us/dotnet/api/system.memoryextensions?view=net-7.0) https://stackoverflow.com/questions/49289559/spanchar-and-string-equality – Drag and Drop Nov 23 '22 at 12:28
  • @DragandDrop sweet thanks you may submit this as an answer and I'll accept it, thanks for pointing this out – Ricky Spanish Nov 23 '22 at 12:30
  • And for reference a bit of performance numbers : https://stackoverflow.com/questions/51864673/c-sharp-readonlyspanchar-vs-substring-for-string-dissection. For answers I any of those link cover 100% of your question , choose it as dupe. If the comment "how to compare" is part of the question , edit it in. and check if it's not a dupe. Anyone can take the mats an redact an answer. just wanted to give a quick feed back with enought source – Drag and Drop Nov 23 '22 at 12:38

0 Answers0