Is C# ever Endian sensitive, for example, will code such as this:
int a = 1234567;
short b = *(short*)&i;
always assign the same value to b. If so, what value will it be?
If not, what good ways are there to deal with endianness if code with pointers in?
C# doesn’t define the endianness. In reality, yes it will probably always be little-endian (IIRC even on IA64, but I haven’t checked), but you should ideally check
BitConverter.IsLittleEndianif endianness is important – or just use bit-shifting etc rather than direct memory access.To quote a few lines from protobuf-net (a build not yet committed):
i.e. it checks the endianness and does a flip if necessary.