Public field X property

Asked

Viewed 218 times

4

I have a string defined as "name" initially it is as private.

string nome;

In case I assign the methods get/set, and to stay in the pattern I change the initial letter to uppercase, getting:

string Nome { get; set; };

Now I can easily access the name variable through the get/set.

To do this which changes I put the code above as follows?

public string Nome

That way he will access equally as if he were get/set, is no longer practical and "clean" so do it this way?

  • Related: https://answall.com/q/293801/112052 and https://answall.com/q/25995/112052

  • 2
  • 1

    This is called encapsulation, an OO concept, gives a read on this and see the links above that posted that you will understand

  • 1

    Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

3 answers

7

In case I assign the methods get/set, and to stay in the pattern I change the initial letter to uppercase, getting:

string Nome { get; set; };

Now I can easily access the name variable through the get/set.

Ok, a lot of people do this, now you will access a private field (yes, it will be private automatically), and in this case the access methods called getter and Setter evening public.

To do this which changes I put the code above as follows:

public string Nome

That way he will access equally as if he were get/set, is no longer practical and "clean" so do it this way?

The first most obvious change is that it finally made the field public, and did not even suspect that the others were not so.

Another important issue is that it is calling attribute because almost everyone doesn’t know the name of it and you were taught wrong. The name of it is field.

Your perception that this may be better is valid and above average. Many people have learned the formula that must use property and accept it as if it were absolute truth without question. Programmer is an engineer, he must be questioner. He has to work as a chef cooking, not a cake recipes cook. You’re on the right track.

In fact in many cases this is the best way. If no action should be taken when you assign a value to the field or when you take a value from it. It’s faster and simpler, and it’s usually what you want.

If you want to place an action, be it a validation, a calculation before delivering a value, or trigger an event, no matter what, then you need the property.

The cool part of C# is that the field and property have identical syntax to access. Then you start using the field and if one day you need to have an extra code in the access, you switch from field to property without problems. Or almost :)

But in your case you’re using the simple property, so why do so?

In simple applications where you compile all of it together it really doesn’t make sense, it’s a waste of time. Even if you have some optimization has no advantage using the expensive property.

But if you create a file Assembly irrespective of other parts of the application, if Assembly is part of a library or framework independent that is used in other applications, there you are stuck if you need to change from field to property. If you change your code and all consumer codes are not changed, it will be a problem.

Depending on how to do it will give error or access the field directly without going through the methods getter and Setter, and it’s no longer what you want. Before it was direct access, now you want this method of proxy. It changes everything. So if you had done it properly from the beginning you wouldn’t need to modify anything in the consumer, you would have a indirect and the internal code, whether it is basic access or with some extra logic, is implementation detail, in any case it will call the method, only what it will do can change according to the code that is in the Assembly of your class where is this field that is never directly accessed.

Some people consider always doing so as a "good practice". And I always say good practice is following a cake recipe without knowing what you’re doing. When you know what you are doing it ceases to be good practice and becomes correctly applied knowledge, even if it is the same thing.

An important note is also that the property can exist independent of a field, it can be just an access method that makes a calculation and gives you. Or it can give a fixed value. And of course, it can involve various fields and even other properties.

Algo como um diagrama de Venn com crenças, verdade, e conhecimento que é um subconjunto das crenças verdadeiras

See more in:

I’m studying because even though I’ve answered this myself, I don’t know if it’s exactly encapsulation as many people think, I may have learned wrong like everyone else. That’s an abstraction, I don’t know if it’s encapsulation. Maybe it’s not, and then we get into another issue that I didn’t cover here is that some say you shouldn’t use methods getter/Setter or properties.

Placa enferrujada com Mythbusted

0

The public allows you to access this variable, or even other classes, methods, etc, through other classes. For example, if you have a variable defined as private, you will not be able to access it from elsewhere, only in the class in which it was defined. The same is true for the methods. Another example is if you create an object, but the class is set to private, you won’t be able to instantiate it when you need it, unless you do it in the class you created it in.

It’s like the name says, public will make the item "public" and private will make it "private".

  • 1

    This I understand, the question is, if I’m going to have the get/set it’s not easier to just make it public so I don’t have to add the get/set ?

  • 1

    You are confusing what is access to the item (variable, method, class...) defined as public or private, with access to value modification and visualization. Set allows you to change the value of the variable, and Get allows you to get that value when you need it. That’s all.

  • yes I understand, but in case you are going to make a change without validation, or simply take a value, because do it private if we can do it public without adding the get/set methods?

  • Now I see where you’re going with this. It is a security issue in this case, getters and setters are adopted as a standard for access to the attributes of a class. It is not good for you to have direct access to the attribute in most cases. An example of this is if you have an integer variable but it cannot be negative, a Setter would solve the problem very easily.

  • yes, but in this case we are talking about treatment, in this case needs the set, now just set a value, it does not become necessary to keep creating several get/set codes because the security will be the same . I have just read, and I will add: https://answall.com/questions/25995/getters-e-setters-s%C3%a3o-uma-Ilus%C3%a3o-do-encapsulation

  • This, exactly. It is not all properties that need, precisely because they do not need it, one must analyze and identify the need or not.

  • 3

    It’s not a security issue. And the explanation in the penultimate comment rolls over and says nothing, actually says the opposite of what was asked, because he said he doesn’t want to do validation and then says that’s what it’s for. Read my answer and understand that almost no one knows why to do it, just do it because someone told you to.

  • @Maniero You’re absolutely right, it’s what was taught in college and just said it was ideal, but not why. Anyway, I am still beginner and gradually I intend to pay attention and change the view that everything that was taught is right and start to question the why of things more often. Your answer clarified much!

  • 3

    When someone doesn’t say why not ignore it. I gave a talk about it this week. If you change the overview, you will proceed well, even if you still have certain vices, because when you don’t even know the way, nothing solves, knowing the rest will solve alone over time. Unfortunately the state of education is so only pass cake recipe and do not produce thinkers.

  • 1

    @Maniero, this phrase is very good "Unfortunately the state of education is so only pass cake recipe and do not produce thinkers".

Show 5 more comments

-1

Within the class you declared no matter if you are string nome; or string Nome { get; set; }.
It’s the same. In the first way, the get/set is implicit, but it’s still there. If you take the set, she would be readonly, then I’d change. The difference will be to access from another class. Declare of the pepper or second way(string nome; or string Nome { get; set; }), it will not be possible to access from another class, only if you put the access modifier as public.

Browser other questions tagged

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