C# C# Streams and Data Processing Parsing Data Working with Integers

Balazs Pukli
Balazs Pukli
46,041 Points

In a class, when do you make a property only, and when do you make a field and a property to that field?

I'm just trippin' about a practical example.

3 Answers

David Lin
David Lin
35,839 Points

If the calculations required to construct your intended return value can be completely confined in a single place (e.g. within a property block), then it makes sense use a property to straightforwardly represent it.

But if the calculations need to be spread out over many methods all over your class, or if it requires some significant amount of time (e.g. due to a network request or database access, etc.), then it might make more sense to create a private field for it to be used solely within the class during value construction, but expose the final value externally as a public property. This gives you the added flexibility to return a default value for the property if the final value hasn't been fully constructed yet.

Balazs Pukli
Balazs Pukli
46,041 Points

Thank you David! This makes a lot of sense! I was wondering because in the universtity, they've teached us to use (private) fields, and access them through property technology. Then, I see that in treehouse courses, it's more likely to use properties only. It made me wonder, how it is in practice, since none of these are "real projects", it's just education.

You seem like you know a lot of what you are talking about! :D I'm on the verge of my developer career, I would appriciate to have a friend who is more experienced. If you are on board, you can have me on facebook.com/puklibalazs . NOTE that I have also a lot of ideas to make startups, maybe having me as a friend will be also beneficial for you one day! :D Thank you for your answer, I appriciate it very much, it made a lot of sense, and it feels like I know more about how programming actually works in practice!

The one thing that is still not clear, how is it even possible for properties to work by themselves. I thought that properties are functions, and that functions need to modify a field, to even store that value in any way. I think properties must be something more, than just functions. They must have a "hidden field" for themselves, to store the value.... what do you think?

David Lin
David Lin
35,839 Points

You're welcome Balaz. Glad to help a little. I don't use FB much, but there're links to my LinkedIn and Google accounts in my profile page if you'd like to connect. Dave

David Lin
David Lin
35,839 Points

I think Properties were introduced as a convenience, since the pattern of creating getter/setter functions for class attributes is extremely common. Properties help to reduce the number of lines of code you need to write to achieve the same thing manually with functions, thus helping to improve code readability. I'm not clear about the lower level details of how properties are actually implemented, but it makes sense that the property name itself becomes effectively a "super-field" in a sense.

Balazs Pukli
Balazs Pukli
46,041 Points

The one thing that is still not clear, how is it even possible for properties to work by themselves. I thought that properties are functions, and that functions need to modify a field, to even store that value in any way. I think properties must be something more, than just functions. They must have a "hidden field" for themselves, to store the value.... what do you think?