As well, is it a compiler trick that I can say
int i = 10;
int j; j=10;
instead of
int i = new System.Int32();
?
The native value types have special treatment due (at least) to their own IL
instructions for loading onto the stack. But if it's any consolation you
can do your own "compiler magic" with conversion operators:
using System;
namespace ConvertableValueType
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
MyValueType value = new byte[] {0xD0, 0x0F, 0x49, 0x40, 0x0, 0x0, 0x0, 0x1};
Console.WriteLine(value.MySingle);
Console.WriteLine(value.MyInt32);
}
}
public struct MyValueType
{
public MyValueType(float fValue, int iValue)
{
MySingle=fValue;
MyInt32=iValue;
}
public float MySingle;
public int MyInt32;
public static implicit operator MyValueType(Byte[] buffer)
{
return new MyValueType(BitConverter.ToSingle(buffer, 0),
BitConverter.ToInt32(buffer, 4));
}
}
}