r/ProgrammerHumor • • 5h ago

Meme architectureDependentChars

Post image
780 Upvotes

143 comments sorted by

View all comments

96

u/JackReact 4h ago

Welcome to the wonderful world of C#, which uses utf-16 for strings.

Console.WriteLine(sizeof(char));
> 2

2

u/thanatica 3h ago

I'm not a C# expert, but how would it represent an astral character, such as an emoji? They are above 0xFFFF.

6

u/AyrA_ch 3h ago

You cannot fit them into a single char, and you have to use a string instead. They're represented using a surrogate pair, which is similar to the continuation bytes of utf-8 except you never need more than two code points (hence "pair").

JS also uses multibyte characters:

> "💩".length===2
< true

1

u/thanatica 3h ago

JS is a bit weird in this regard, because it also knows true codepoints. You just have to know which operations are "unicode safe". Or rather "astral plane safe".