string.Reverse() landmine #131609
|
"𝄞".Reverse() does something broken. On investigation it's not string.Reverse() but IEnumerable.Reverse() and appears to be converted back to a string in some contexts. Clearly trying to reverse a string has a fools trap in it. Oversight? |
Replies: 2 comments 2 replies
|
You can't generally reverse a string for essentially anything outside the basic ASCII range, which includes the treble-clef character in your example. This is true both for the UTF-16 encoding used natively with strings in C#, and the idealized character concepts present in Unicode (see combining characters). Thus, there is no "Reversing" a string is a non-trivial operation, and is use/context specific. |
Well; Intellisense thinks there is. :( |
Notably this is just a side effect of
extension methods(and with latest C#,extension members).Anyone can add extensions for any type and when you match against an interface, it can expose functionality that may not be 100% sensible for the concrete type and rather only makes sense for the thing being matched against, which in this case is
IEnumerable<char>and soReversesimply enumerates the individual UTF-16 characters in reverse.IntelliSense explicitly surfaces and allows filtering between concrete members and extension members.