Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 531
whossname Avatar asked Jun 19 '13 03:06

whossname


People also ask

What is the main cause of hep C?

Hepatitis C is a liver infection caused by the hepatitis C virus (HCV). Hepatitis C is spread through contact with blood from an infected person. Today, most people become infected with the hepatitis C virus by sharing needles or other equipment used to prepare and inject drugs.

Does hep C go away?

Hepatitis C virus (HCV) causes both acute and chronic infection. Acute HCV infections are usually asymptomatic and most do not lead to a life-threatening disease. Around 30% (15–45%) of infected persons spontaneously clear the virus within 6 months of infection without any treatment.

What does hep C pain feel like?

Many people with chronic HCV suffer from aches and pains in their joints. A variety of different joints can be involved but the most common are in the hands and wrists. These pains are often minor but occasionally the pain can be quite severe. In such cases painkillers can be used to relieve the symptoms.

How easy is it to get hep C?

Hepatitis C is spread only through exposure to an infected person's blood. High-risk activities include: Sharing drug use equipment. Anything involved with injecting street drugs, from syringes, to needles, to tourniquets, can have small amounts of blood on it that can transmit hepatitis C.


1 Answers

The constant Int32.MaxValue is stored at compile time, and in fact your code would be converted to Convert.ToDouble(0x7FFFFFFF) at compile time. The equivalent IL is:

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.

As minitech suggests, (double)Int32.MaxValue is evaluated at compile-time. The equivalent IL is:

ldc.r8      00 00 C0 FF FF FF DF 41 
like image 79
p.s.w.g Avatar answered Oct 31 '22 17:10

p.s.w.g