Well, just test it with IlSpy and for the code:
private static void Main(string[] args) { int value = 365 * 24 * 60 * 60; Console.WriteLine(value); }
Compiled MSIL Code:
..... IL_0000: nop IL_0001: ldc.i4 31536000 // its calculated already IL_0006: stloc.0 IL_0007: ldloc.0 IL_0008: call void [mscorlib]System.Console::WriteLine(int32) IL_000d: nop IL_000e: ret } // end of method Program::Main
Therefore, it computes a static expression at compile time to improve performance, but if we change our code to:
double nbr = Math.Sqrt(365 * 24 * 60 * 60); Console.WriteLine(nbr);
This time, the final result will be filled at runtime because the compiler does not know the value that Math.Sqrt() will return at compile time.
MsIL Code:
IL_0000: nop IL_0001: ldc.r8 31536000 IL_000a: call float64 [mscorlib]System.Math::Sqrt(float64) IL_000f: stloc.0 IL_0010: ldloc.0 IL_0011: call void [mscorlib]System.Console::WriteLine(float64) IL_0016: nop IL_0017: ret } // end of method Program::Main
source share