static int valor1 = 10 / 5;
This is a static variable, the compiler will probably do the calculation and store the result in static space.
static int valor2() => 10 / 5;
Here is a static method, again there may be an optimization with the ready calculation stored in static area, but it is less likely, then your invocation will perform a simple algorithm. As the method is internal and there are guarantees that it can not be accessed from outside it is possible that the method is optimized and a call to the direct value is placed in place of the method call.
static int valor3 => 10 / 5;
Here is a property, ie a pair of access methods (in this case only the get
) that will execute the operation when it is called. It is possible that an optimization is done as in the method.
Optimizations are not in specification, it's just a possibility. Currently this occurs:
C..cctor()
L0000: push ebp
L0001: mov ebp, esp
L0003: mov dword [0x1609da6c], 0x2
L000d: pop ebp
L000e: ret
C.valor2()
L0000: mov eax, 0x2
L0005: ret
C.get_valor3()
L0000: mov eax, 0x2
L0005: ret
Only returns% with% in all 3 cases as expected. But I thought I could have more optimization for being an internal, not a public member.
See the SharpLab .