问题
Or is it always guaranteed to be positive for all possible Chars?
回答1:
It's guaranteed to be non-negative.
char
is an unsigned 16-bit value.
From section 4.1.5 of the C# 4 spec:
The
char
type represents unsigned 16-bit integers with values between 0 and 65535. The set of possible values for thechar
type corresponds to the Unicode character set. Althoughchar
has the same representation asushort
, not all operations permitted on one type are permitted on the other.
回答2:
Since the range of char is U+0000 to U+ffff, then a cast to an Int32
will always be positive.
回答3:
Each 16-bit value ranges from hexadecimal 0x0000 through 0xFFFF and is stored in a Char structure.
Char Structure - MSDN
回答4:
See Microsoft's documentation
There you can see, that Char is a 16 bit value in the range of U+0000 to U+ffff. If you cast it to a Int32, there should be no negative value.
回答5:
char
can be inplicitly converted to ushort
and ushort
range is 0 to 65,535
so its always positive
来源:https://stackoverflow.com/questions/7820365/in-c-sharp-can-casting-a-char-to-int32-produce-a-negative-value