This question and the answers are quite old, and the language and standard library have evolved quite a bit since it was originally asked, but I will supply a more "modern" answer that is applicable for the times.
using System.Runtime.CompilerServices;
int color = -2451337;
uint unsigned = Unsafe.As<int, uint>(ref color);
The advantage here is that there is no boxing or cast, which is still occurring even when using unchecked
. This merely provides a different way of looking at the same bits, analogous to reinterpreting a pointer type in a language like C.
Contrary to the name of the class being Unsafe
, this is not considered unsafe
code, and also works well for avoiding boxing of enums when converting to/from integral types, though one must ensure that the enum is the same number of bits as the integer type.
There is some discussion of this technique by the language developers here.