Difference between properties syntax in C#

Asked

Viewed 318 times

3

For what I researched, if I do:

public int numero { get; set; }

is the same thing as:

private int numero;

public int Numero {
    get{
        return numero;
    }

    set{
        numero = value;
    }
}

But in the first chunk of code everything gets access public, in the second the attribute is declared as private and the get and set sane public, There’s really no difference?

  • Not because what can be accessed publicly is exactly the same in both cases - property Numero

  • For what explains in the link below is not attribute but characteristics of an object.(Or variable of class or instance) : Property x Attribute

  • I don’t quite understand the difference either so I have the same question

  • Not the same as in java ? private String name; public void setName(String name) { this.name=name; }

3 answers

7


According to the documentation, the excerpts 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;
        }
    }

}

I mean, this way you’re creating a field name private and a property Name public accessing the field name. But if no logic is needed in implementing field accessors, getand set, there is no need to write all this code above. From C# version 3.0 it is possible to do only:

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

With this syntax, C# will create the property Name defining the methods get and set as in the example above, however, it 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 accessors get and set of property.

This syntax is called self-applied property. An interesting reading on the subject is:

Property Vs Variables

In fact, this is even duplicate of this question.

Documentation

Using Properties (Programming Guide in C#)

Self-implemented Properties (Programming Guide in C#)

5

The first code is a syntactic sugar for the second, roughly speaking. So there is no difference between them, this you know. If it is the same thing, why doubt? The first has everything you have in the second, you just don’t see written there.

If writing the first is the same as writing the second, it is obvious that there is no difference. You might want to see something else that doesn’t exist there.

There is a private field in the first one too, after all where it will keep the die? It needs to be in a class variable (static or instance, in case it is instance). Just because you didn’t type or see doesn’t mean you’re not there.

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

By the way, a lot of people think a line of code can be faster than several lines. This is obviously not true, what lies 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 hi how much a processing costs, each instruction its different cost.

See how the generated code looks:

[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 on Sharplab.

  • Can’t do it either? private int _numero; public void setName(int _numero) { this. _numero=_numero; }

  • 1

    Yes, but that’s not idiomatic, there are drawbacks.

  • okay then, I get it

  • I was going to ask about the difference between defining a property like this and keeping the countryside public, but I researched and ended up finding this question. In the end, I think it’s even duplicate.

1

I agree that it is a syntactic sugar. In practice (for me, okay?) I use the get;set; with nothing when I just need to assign the property "as is". When it is necessary to do some kind of treatment (like format the output, for example) I declare the private Fields and treat them in get and set methods. I do it just so I can standardize interactions with the property without having to change a zillion probable classes.

Other than that, there’s no difference in performance, etc. You just write a little more.

I hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.