Is casting char to int a safe operation in C#?
.net, c#
Solution
A conversion from `char` to `int` will not fail, with any `char` value.
From .NET 4.0 reference
The .NET Framework uses the Char structure to represent a Unicode character. The Unicode Standard identifies each Unicode character with a unique 21-bit scalar number called a code point, and defines the UTF-16 encoding form that specifies how a code point is encoded into a sequence of one or more 16-bit values. Each 16-bit value ranges from hexadecimal 0x0000 through 0xFFFF and is stored in a Char structure. The value of a Char object is its 16-bit numeric (ordinal) value.
Problem
Given the following code: ``` string source = "Some Unicode String"; foreach( char value in source ) { int y = (int)value; } ``` Is it possible that the cast from char to int could fail (and under what circumstances)?