Jump to content

Compile-time calculation: Difference between revisions

(→‎{{header|Visual Basic .NET}}: Moved Compiler note to correct location)
Line 251:
leave
ret</lang>
 
=={{omit fromheader|C sharp}}==
'''Compiler:''' Roslyn C#, language version 7.3
 
The Roslyn compiler performs constant folding at compile-time and emits IL that contains the result.
 
<lang csharp>using System;
 
public static class Program
{
public const int FACTORIAL_10 = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1;
static void Main()
{
Console.WriteLine(FACTORIAL_10);
}
}</lang>
 
{{out|Emitted IL|note=disassembled with ILSpy}}
 
<!--CIL assembly has a similar overall syntax to C#, so colorize it as such.-->
<lang csharp>.class public auto ansi abstract sealed beforefieldinit Program
extends [System.Runtime]System.Object
{
// Fields
.field public static literal int32 FACTORIAL_10 = int32(3628800)
 
// Methods
.method private hidebysig static
void Main () cil managed
{
// Method begins at RVA 0x2050
// Code size 11 (0xb)
.maxstack 8
.entrypoint
 
IL_0000: ldc.i4 3628800
IL_0005: call void [System.Console]System.Console::WriteLine(int32)
IL_000a: ret
} // end of method Program::Main
 
} // end of class Program</lang>
 
Note that the constant field is generated only when both it and the containing class are visible outside of the assembly.
 
Constant expressions that appear outside of constant declarations are also folded, so
 
<lang csharp>using System;
 
static class Program
{
static void Main()
{
Console.WriteLine(10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1);
}
}</lang>
 
and
 
<lang csharp>using System;
 
static class Program
{
static void Main()
{
int factorial;
factorial = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1;
Console.WriteLine(factorial);
}
}</lang>
 
 
produce the same IL, except without the field.
 
{{out|Emitted IL|note=disassembled with ILSpy}}
 
<lang csharp>.class private auto ansi abstract sealed beforefieldinit Program
extends [System.Runtime]System.Object
{
// Methods
.method private hidebysig static
void Main () cil managed
{
// Method begins at RVA 0x2050
// Code size 11 (0xb)
.maxstack 8
.entrypoint
 
IL_0000: ldc.i4 3628800
IL_0005: call void [System.Console]System.Console::WriteLine(int32)
IL_000a: ret
} // end of method Program::Main
 
} // end of class Program</lang>
 
{{out}}
<pre>3628800</pre>
 
=={{header|Clojure}}==
Line 1,138 ⟶ 1,234:
 
<!--CIL assembly has a similar overall syntax to C#, so colorize it as such.-->
<lang Csharpcsharp>.class private auto ansi sealed Program
extends [System.Runtime]System.Object
{
Line 1,187 ⟶ 1,283:
 
{{out|Emitted IL|note=disassembled with ILSpy}}
<lang Csharpcsharp>.class private auto ansi sealed Program
extends [System.Runtime]System.Object
{
Line 1,297 ⟶ 1,393:
{{omit from|Bc}}
{{omit from|Brlcad}}
{{omit from|C sharp}}
{{omit from|E}}
{{omit from|Gambas}}
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.