Difference between syntax of properties in C # [duplicate]

3

What I researched, if I did:

public int numero { get; set; }

is the same as:

private int numero;

public int Numero {
    get{
        return numero;
    }

    set{
        numero = value;
    }
}

But in the first code snippet everything gets access public , in the second the attribute is declared as private and the get and set are public , this does not have really difference?

    
asked by anonymous 20.07.2017 / 23:29

3 answers

7

According to the documentation, the snippets are (practically) equivalent. In the second, you define a class field as private and create a property to manage it.

public class Person
{

    // Define o campo 'name':
    private string name;

    // Define a propriedade 'Name':
    public string Name
    {
        get
        {
            return name;
        }

        set
        {
            name = value;
        }
    }

}

That is, in this way you are creating a name private field and a Name public property that accesses the name field. But if no logic is needed in the implementation of the field accessors, get and set , there is no need to write all that code above. From version 3.0 of C # you can only do:

public class Person
{
    public string Name { get; set; };
}

With this syntax, C # will create the Name property by defining the get and set methods as in the example above, but will also create a private and anonymous field for support that will be accessed by such methods. See documentation :

  

When declaring a property, the compiler creates a private and anonymous support field that can be accessed only through the get and set accessors of the property.

This syntax is called self-implemented property. An interesting read on the subject is:

Vs Variable Property

Actually, this is even a duplicate of this question.

Documentation

Using Properties (C # Programming Guide)

Self-Completed Properties (Programming Guide C #)

    
21.07.2017 / 00:06
4

The first code is a syntactic sugar for the second, roughly. So there's no difference between them, this you know. If it is the same, why doubt? The first one has everything it has in the second, you just can not see it written there.

If you write the first one is the same as the one you wrote the second one, it is obvious that it has no difference. You may be wanting to see something else that does not exist there.

There is a private field in the first too, after all where will it save the die? Must be in a class variable (static or instance, in case it is instance). Just because you did not type or do not see does not mean it is not there.

The code for get and set are methods that execute a code, however simple they are, but do not store values, only the field can store a value. Both are public.

Alias, a lot of people think that one line of code can be faster than several lines. This is obviously not true, what is behind that line is what defines what it does. What you see is always an abstraction of something greater. In fact, not even an Assembly line defines how much a processing costs, each instruction its different cost.

See what the generated code looks like:

[DebuggerBrowsable(DebuggerBrowsableState.Never), CompilerGenerated]
private int <numero>k__BackingField; //o campo privado criado pelo copilador

private int _numero; //o cam criado manualamente

public int numero
{
    [CompilerGenerated]
    get
    {
        return this.<numero>k__BackingField; //o acesso criado pelo compilador
    }
    [CompilerGenerated]
    set
    {
        this.<numero>k__BackingField = value; //a mutação criada pelo compilador
    }
}

public int Numero
{
    get
    {
        return this._numero;
    }
    set
    {
        this._numero = value;
    }
}

See the

21.07.2017 / 00:00
1

I agree that it is a syntactic sugar. In practice (for me, ok?) I use get; set; without anything when I just need to assign the property "as is". When it is necessary to do some type of treatment (like formatting the output, for example) I declare the private fields and treat them in the get and set methods. I do this only to be able to standardize the interactions with the property without having to change a zillion of likely classes.

Otherwise, there is no difference in performance, etc. You just write a little more.

I hope I have helped!

    
21.07.2017 / 00:06