Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is BIGENDIAN a directive if it's not resolved at compile-time?

Excuse the poor wording, but I couldn't find a better way to explain it.

From my understanding, C# is a WORA language- you can write it on one machine and deploy it on another, because the MSIL isn't compiled until the application is actually run.

So then why is it that BitConverter.IsLittleEndian is defined like so:

#if BIGENDIAN
    public static readonly bool IsLittleEndian /* = false*/;
#else
    public static readonly bool IsLittleEndian = true;
#endif

BIGENDIAN here is a preprocessor directive, which means that it's resolved at compile-time. So if the developer's machine is little endian, and the target uses big endian, will IsLittleEndian still report true on the target machine?

like image 696
James Ko Avatar asked Sep 23 '15 18:09

James Ko


1 Answers

No, it will work as expected. The reason it'll work is because the .NET runtime installed on the target system was built/compiled for that target system, so it's BitConverter.IsLittleEndian property will return false. Your code is simply referencing that property, so it isn't determined until runtime.

like image 127
Mayoor Avatar answered Oct 14 '22 00:10

Mayoor