What's the difference in the 3 types of variables and how do they behave in the compiler?

12

For test questions, I made the following code:

static int valor1 = 10 / 5;

static int valor2() => 10 / 5;

static int valor3 => 10 / 5;

public static void Main()
{
    Console.Write($"{valor1}\n{valor2()}\n{valor3}");
}

And the output was:

2
2
2

That is, there was no difference in the result. With that, my doubts are:

  • What is the difference in the 3 types of variables presented (if they are variables)?

  • Do all behave the same way in the compiler?

Running here .

    
asked by anonymous 04.09.2017 / 12:51

3 answers

12
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 .

    
04.09.2017 / 13:21
11
static int valor1 = 10 / 5;    // Declaração de um campo.

static int valor2() => 10 / 5; // Declaração de um método estático utilizando membro de expressâo incorporada.

static int valor3 => 10 / 5;   // Declaração de uma propriedade utilizando membro de expressão incorporada.

The main difference is noted by the use of embedded expression members, which is nothing more than syntactic introduced in version 6 of C #.

The compiler to convert the following code (see for yourself by using the tool SharpLab ):

private static int valor1 = 2;

private static int valor3
{
    get
    {
        return 2;
    }
}

private static int valor2()
{
    return 2;
}

References:

04.09.2017 / 13:37
6

In reality you are creating variables and methods and assigning values to them differently.

As in the first.

static int valor1 = 10 / 5;

Here you declare the variable valor1 as an integer and static and assign the value of a mathematical function to it 10 / 5 .

In the second;

static int valor2() => 10 / 5;

You are creating a method and assigning a value through an expression (=>), you are not using a variable to access its value, but calling the valor2() method.

In the third;

static int valor3 => 10 / 5;

You create a variable and assign the value to it by the expression => .

  

What is the difference in the 3 types of variables presented (if   variables)? Do all behave the same way in the compiler?

As shown above these are the differences.

  

Do all behave the same way in the compiler?

No, as I have specified each one has a different behavior perincipally the second the others have the way the value was assigned.

    
04.09.2017 / 13:23