I would strongly recommend only using u
, as it's much less error-prone.
x
consumes 1-4 characters, so long as they're hex digits - whereas u
must always be followed by 4 hex digits. From the C# 5 specification, section 2.4.4.4, the grammar for x
:
hexadecimal-escape-sequence:
x
hex-digit hex-digitopt hex-digitopt hex-digitopt
So for example:
string good = "Tabx9Good compiler";
string bad = "Tabx9Bad compiler";
... look similar but are very different strings, as the latter is effectively "Tab" followed by U+9BAD
followed by " compiler".
Personally I wish the C# language had never included x
, but there we go.
Note that there's also U
, which is always followed by 8 hex digits, primarily used for non-BMP characters.
There's one other big difference between u
and x
: the latter is only used in character and string literals, whereas u
can also be used in identifiers:
string x = "just a normal string";
Console.WriteLine(u0078); // Still refers to the identifier x
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…