C# properties for those who know Java

Asked

Viewed 119 times

6

What a Java programmer needs to know about basic properties (properties) in order to read code written in C#?

2 answers

5


Property is only one syntax sugar for the methods getter and Setter which is commonly used in Java and other languages. So you write the access in the code as if it were a field, but you’re actually accessing one of the access methods to take or change the value of a field, probably private.

It can do more than that (have the logic it thinks best, as it can in Java) or it may not use a field to do its operation.

In my opinion it is more readable and more elegant, in addition it gives a better understanding to what serves a getter and Setter, and facilitates the option. There are abuses, not everything should be a property, as well as abuse of getters and setters where you should not.

Almost everything has been answered before. I think you can start by How the properties in C work#?.

Then you can see when using a method and not a property. And when using a variable instead. More in Public field X property.

It can be initialized nominally.

There are restrictions, but can inherit. Can also be used from static form.

Her can be declared in interface normally, but it would need to be realized in the class that implements the interface.

It might be interesting to know that the set may have another visibility.

In the next versions you can even allow only a boot to be done, but do not let it change later, which can prevent the creation of a constructor if you do not want to do anything extra besides the initialization of its members and do not need a specific order.

Initialization can already be done in the property and not in the constructor, at least not explicitly (in fact every boot is always done in the constructor, there is no way to execute algorithms outside a method, so it is mandatory and the compiler creates one with the initializations needed for you.

See how it simplifies the statement in Why auto properties exist in C#?. Another form in How to use expanded property in C#.

Her can even be used as an indexer and replace the method get() (and eventually a set()) of the class element that are collections.

You can see some internal issues of how it works:

Anyway, I can talk about a lot, I I have answered this in several questions.

3

Property is a type that has the getter and Setter embedded in it. Instead of creating a public getter and Setter in a class to access/change private types, Oce creates a Property.

public class Carro
{
   public string Nome
   {
      get;
      set;
   }

   public Carro( string nome )
   {
       Nome = nome;
   }

}

var meuCarro = new Carro( "Model S" ); 
Console.Write( meuCarro.Nome ) // vai imprimir "Model S"

// mudando o valor do property, em outras linguages de programacao voce usaria o methodo "set()" explicitamente 
meuCarro.Nome = "Model 3"

You can also put logic inside the getter and Setter of a Property in C#, so the value Voce is accessing or modifying may be changing in the process.

  • did not understand why the output will be "bmw"

  • 1

    @Caiohenriquereblin error, originally had put "bmw", changed in the code but not in the comment! Thanks for the remark

Browser other questions tagged

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