How do C#properties work?

Asked

Viewed 354 times

9

In Java I usually create private attributes and create methods getters and setters, on account of the encapsulation. I couldn’t quite understand how this works in the C#.

  • What exactly you did not understand, could put a part with example of this?

2 answers

9


In C# what you call an attribute is treated as a field, since attribute is something else in language (something similar to the Java annotation). In fact after study I saw that in almost all languages the correct name is not attribute.

Although it can do exactly the same in C#, it is not idiomatic. C# has a syntactic sugar that facilitates its use. In the background what is being done internally is to create a field and a pair (may even be only one) of methods (get and set). What changes is in the call.

Instead of calling one

x + objeto.getValor()
objeto.setValor(10)

is used

x + objeto.Valor
objeto.Valor = 10

The statement of that would be something like this:

public int Valor { get; set; }

Which is the same as writing:

private int valor;
public int {
    get { return valor; }
    set { valor = value; }
}

Note that value is a contextual keyword, it is the parameter that receives the value to be setate. This longer syntax is only used when you need to create a specific logic different from the pattern.

The same code in Java would be something like this:

private int valor;
public int getValor() {
    return valor;
}
public void setValor(int value) {
     valor = value;
}

Obviously, like Java, you can put any logic in there. If you understand that it is a method, it is easy to understand its use.

It is possible to indicate that the set be private and can put an initial value (C# 6 up). This example creates a property that cannot be changed externally:

public int Valor { get; private set; } = 10;

Or it could be all immutable:

public int Valor { get; } = 10;

Ideally in this case the initialization should be in a parameterized form constructor. In the way done there a static and read-only property would be better, since the value is not parameterizable and never changes. But that’s a matter for building.

In C# 9 you have the possibility to only initialize the property and get, but do not change the value after startup:

public int Valor { get; init; };

I put in the Github for future reference.

As properties can be initialized in construction even where there is no constructor. But there may be semantic differences therein.

I have already answered on the subject in several questions:

  • @bigown so when I declare, for example: public int Numero { get; set; } Is this property private and the public get and set methods? What defines public property then?

  • @elvex The field is private, and the methods (which do not look like methods) are public, so this is public property. This is public property, which by the way makes little or no sense to create private property, even if it can. You may have a terminology problem there. If you are talking about the public field you can also do it if you like, like Java. I suggest reading the links placed to pick up more details.

  • @Mustache I’ll read the links as soon as possible. But what I meant was: in Java I usually create attributes (C#properties) with the private modifier and get/set methods with public. That is, public methods and private attribute. In C# is already declared the attribute with public. From what I understand, this makes the methods public. What I wanted to understand is: does the property also become public? Is access to it direct? Or is it always by get/set?

  • 1

    @elvex Properties in C# are only a syntactic sugar for the use of get/set in a more elegant way, allowing to use a more user-friendly syntax. And the properties have a self-application by default, if you don’t specify how get/set should work, it creates a private field and works with it in a simple way. You can see here (http://stackoverflow.com/questions/23102639/are-c-sharp-properties-actually-methods) how a self-applied property turns into an equivalent C# code with methods and fields.

  • It is worth remembering that, since C# 6, it is possible to use Lambdas to define read-only properties (public int SegundosAgora => {DateTime.Now.Seconds})

5

C# simplifies some of this for you, although underneath it all, it’s exactly the same.

You set an access modifier for the attribute (in C#, we call property) in general

public string Nome { get; set; }

Name will have his getter and Setter public.

You can also set separately

public string Nome { get; private set; }

In this case, Name will have the Setter private and getter public.

  • Ok, I have changed and I will delete comments that I do not believe to be valid now.

Browser other questions tagged

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