While writing conversion of generic enum to int strange things happened around unsafe read of sbyte type to byte.
The folloging examples were tested with .Net 6.0 on AMD x64 machine.
Example 1: Inconsistency Debug vs. Release
The following code generates different output in Debug and in Release mode:
class Program
{
static void Main()
{
byte byteValue = ReadAsByteValue(sbyteValue: -1);
Console.WriteLine(byteValue);
// OUTPUT DEBUG: 255
// OUTPUT RELEASE: -1
}
static unsafe byte ReadAsByteValue(sbyte sbyteValue)
{
return *(byte*)(&sbyteValue);
}
}
Since type byte
does not have value -1, I suppose that in Release mode the compiler returns sbyte
instead of byte
.
Example 2A: Inconsistency in Release mode
class Program
{
static void Main()
{
var value1 = GetIntValueEncapsulated((sbyte)-1, true);
var value2 = GetIntValue((sbyte)-1);
Console.WriteLine($"{value1} vs. {value2}");
foreach (var value in Array.Empty<sbyte>())
{
GetIntValueEncapsulated(value, true);
}
// OUTPUT RELEASE: -1 vs. 255
}
static int GetIntValueEncapsulated<T>(T value, bool trueFalse)
where T : unmanaged
{
if (trueFalse)
{
return GetIntValue(value);
}
else
{
throw new NotImplementedException($"Not implemented for size: {Unsafe.SizeOf<T>()}");
}
}
static unsafe int GetIntValue<T>(T value)
where T : unmanaged
{
return *(byte*)(&value);
}
}
Example 2B: Commenting out empty foreach
changes results
var value1 = GetIntValueEncapsulated((sbyte)-1, true);
var value2 = GetIntValue((sbyte)-1);
Console.WriteLine($"{value1} vs. {value2}");
//foreach (var value in Array.Empty<sbyte>())
//{
// GetIntValueEncapsulated(value, true);
//}
// OUTPUT RELEASE: -1 vs. -1
Example 2C: Non-functional change on the Exception line changes results
Starting with Example 2A and replacing line:
throw new NotImplementedException($"Not implemented for size: {Unsafe.SizeOf<T>()}");
with line:
throw new NotImplementedException($"Not implemented for size: " + Unsafe.SizeOf<T>());
gives output:
// OUTPUT RELEASE: 255 vs. 255
Questions
- What is the exact cause of these differences?
- How to force compiler in the Release mode to behave as expected? (i.e. as in the Debug mode)