In C# can casting a Char to Int32 produce a negative value?

后端 未结 5 931
时光取名叫无心
时光取名叫无心 2021-01-18 11:03

Or is it always guaranteed to be positive for all possible Chars?

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-18 11:49

    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 the char type corresponds to the Unicode character set. Although char has the same representation as ushort, not all operations permitted on one type are permitted on the other.

提交回复
热议问题