-6

Although I understand the basic concept of properties like providing read, read-write access to private data members, I am still having a hard time understanding how it would be useful over just declaring the member as public. In what scenarios is it useful? and if it is a way to change values of private fields, how is the encapsulation still being enforced?

Kindly explain with an example or link if you can

  • This sounds like it belongs on an exam... – Mikanikal May 24 '17 at 19:41
  • 1. The `get` and `set` can do more than just get or set a private field. 2. Sometimes, a framework *requires* properties to be used: WPF bindings, for example, will not work on fields. – 15ee8f99-57ff-4f92-890c-b56153 May 24 '17 at 19:42
  • 2
    `Kindly explain with an example or link if you can` You don't know how to google? Like [this](https://softwareengineering.stackexchange.com/questions/143736/why-do-we-need-private-variables) – Rick S May 24 '17 at 19:43
  • 2
    Take your pick as far as potential duplicates: [Why we need Properties in C#](https://stackoverflow.com/q/1523548/150605), [Should I use public properties and private fields or public fields for data?](https://stackoverflow.com/q/1277572/150605), [Properties vs. Fields: Need help grasping the uses of Properties over Fields](https://stackoverflow.com/q/3069901/150605), and so on... – Lance U. Matthews May 24 '17 at 19:53

2 Answers2

0

I think there is a bit of confusion on properties vs fields, and private vs. private (vs. internal)

Fields are very much like plain variables of a class. They can be public or private. Properties, just like fields, can also be public or private. However, while they appear to behave similar to a field, they actually behave more like functions with a particular signature (signature being setter taking one single parameter of the type of the property, and getter taking no parameters and returning that type). Because they behave like a function, whenever you set or retrieve property's value, you can run arbitrary code to fulfill the behavior (i.e. caching the value, and if cache is empty, retrieving value from somewhere).

LB2
  • 4,802
  • 19
  • 35
-1

From personal experience:

You would generally have Private data member when you do not want it to be accessed externally through another class that calls the class containing the Private data member.

Public data members are those that you can access by other classes to obtain its contents.

My opinion is that it is simply proper programming syntax. Private data members are typically those of constants that you do not wish to override once it had been set, while Public are algebraic-like variables, subject to be overridden if necessary.

A similar question has been asked at: What is the difference between Public, Private, Protected, and Nothing?.

Cheers,

iato

iato
  • 356
  • 5
  • 16
  • I don't understand how this answers the question. The question is about why it's advantageous to use properties instead of fields. This answer and the one it links to is about using `public` vs. `private` access modifiers. They're completely different concepts. This answer doesn't even mention fields or properties, just "data members" in general. – Lance U. Matthews May 24 '17 at 21:04