I am passing 0xffff0000 to a DirectX function that takes an int and interprets the int as a color (fully transparent red). The C# compiler says this is an error because it is auto-converting 0xffff0000 to a uint and saying it can't convert the value to an int (which is what the function takes). Is the C# compiler disallowing overflow? This wouldn't be a problem in C/C++ because the function looks at the byte content of the parameter anyway. Is there a solution to this?
By the way, is there a good resource (web or book) where I can find gory C# details like this? Something like Stroustrup's C++ book?
Thanks
AlanPosted Nov 29, 2008, 3:31 PM
It seems to be the only way you can get it through the compiler. I wondered whether compiling with the /checked- switch (see link below) might enable you to avoid using unchecked but it only seems to affect runtime behaviour:
http://msdn.microsoft.com/en-us/library/h25wtyxf(VS.71).aspx
Daniel RobertsPosted Nov 29, 2008, 11:52 AM
Thanks
AlanPosted Nov 29, 2008, 6:45 AM
The rule with integer literals in C# is that they're treated as type 'int' if they're within the int range, otherwise they're treated as type 'uint'.
However, you can force them to 'wrap' using the unchecked function:
int x = unchecked((int)0xffff0000);
Console.WriteLine(x); // -65536
The best place to look for these sort of details is the C# Specification :
http://msdn.microsoft.com/en-gb/vcsharp/aa336809.aspx