简体   繁体   English

C#编译器是否转换并存储静态变量?

[英]Does C# compiler convert and store static variables?

My program uses the code: 我的程序使用代码:

Convert.ToDouble(Int32.MaxValue)

Fairly regularly. 相当经常。 I was just curious about how this is handled by the compiler. 我只是好奇编译器如何处理它。 Is it stored as a static double or is it executed in run time? 它是作为静态双精度存储还是在运行时执行?

The constant Int32.MaxValue is stored at compile time, and in fact your code would be converted to Convert.ToDouble(0x7FFFFFFF) at compile time. 常量Int32.MaxValue在编译时存储,实际上您的代码将在编译时转换为Convert.ToDouble(0x7FFFFFFF) The equivalent IL is: 等效的IL是:

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

This value is also saved so it can be retrieved at run-time through reflection. 此值也会保存,以便可以在运行时通过反射检索它。

However, Convert.ToDouble is a function that is only evaluated at run-time. 但是, Convert.ToDouble是仅在运行时评估的函数。

As minitech suggests, (double)Int32.MaxValue is evaluated at compile-time. 正如minitech建议的那样, (double)Int32.MaxValue在编译时进行评估。 The equivalent IL is: 等效的IL是:

ldc.r8      00 00 C0 FF FF FF DF 41 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM