Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

casting

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

like image 382
kaalus Avatar asked Oct 19 '11 11:10

kaalus


1 Answers

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.

like image 109
Jon Skeet Avatar answered Nov 14 '22 22:11

Jon Skeet