C # compiler convert and save static variables?

My program uses the code:

Convert.ToDouble(Int32.MaxValue) 

Pretty regular. I was just wondering how this is handled by the compiler. Is it stored as a static double or is it executed at runtime?

+6
source share
1 answer

The Int32.MaxValue saved at compile time, and in fact your code will be converted to Convert.ToDouble(0x7FFFFFFF) at compile time. Equivalent IL:

 ldc.i4 FF FF FF 7F call System.Convert.ToDouble 

This value is also stored, so it can be obtained at run time through reflection.

However, Convert.ToDouble is a function that is evaluated only at runtime.

As minitech shows, (double)Int32.MaxValue is evaluated at compile time. Equivalent IL:

 ldc.r8 00 00 C0 FF FF FF DF 41 
+8
source

Source: https://habr.com/ru/post/947611/


All Articles