static int valor1 = 10 / 5;
This is a static variable, probably the compiler will do the calculation and store in static area of memory the result.
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, so your invocation will run a simple algorithm. Since the method is internal and there are guarantees that it cannot 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 couple of access methods (in case you will only have the get
) that will perform the operation when called. It is possible that an optimization is done as in the method.
I put in the Github for future reference.
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
Just returns 2
in the 3 cases as predicted. But I thought I could get more optimization by being an internal member and not public.
See on Sharplab.
Everything seems whole to me, because you think there would be differences?
– user28595
@Article The form in which they are created.
– Francisco