I wrote a small piece of code to demonstrate this problem. The problem is that when I declare a structure with a state, Layout.Explicit
it assigns undefined values to the fields of this structure depending on another value. This problem only occurs when using state Layout.Explicit. this is pretty hard to explain without code, so here is a short example.
using System.Runtime.InteropServices;
namespace ConsoleStruct
{
class Program
{
[StructLayout(LayoutKind.Explicit)]
struct TestStruct
{
[FieldOffset(0)]
public double dbl;
[FieldOffset(0)]
public ulong uu;
}
public static void SimpleMethod()
{
TestStruct st;
st.uu = 0;
st.dbl = 5000.0;
Console.WriteLine(st.uu.ToString());
Console.ReadLine();
}
static void Main(string[] args)
{
SimpleMethod();
}
}
}
Can someone explain to me why this is happening. Using VS 2013. Thanks.
source
share