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.
Not because what can be accessed publicly is exactly the same in both cases - property
Numero
– ramaral
For what explains in the link below is not attribute but characteristics of an object.(Or variable of class or instance) : Property x Attribute
– Marconi
I don’t quite understand the difference either so I have the same question
– Amadeu Antunes
Not the same as in java ? private String name; public void setName(String name) { this.name=name; }
– Amadeu Antunes