Encapsulating data in C # using properties

I'm currently thinking about encapsulating data in C #, and I'm a bit confused. A few years ago, when I began to study programming in C ++, my professor told me: - "Create a class and hide its data elements, so it cannot be manipulated directly from the outside"

Example: You parse an XML file and store the parsed data in some data elements inside the parser class.

Now when I look at C #. You have properties. This function makes the internal state / internal data of the class visible from the outside. No more encapsulation. Correctly?

private string _mystring; public string MyString { get {return _mystring;} set {_mystring = value;} } 

From my point of view, there is no difference between posting data or public properties that getters and setters have, where you pass your personal data through.

Can someone please explain to me?

thanks

+6
c # properties encapsulation member
source share
7 answers

Private data is encapsulated by the property itself. The only way to access data is through a property.

In the above situation, it makes no sense to use a property. However, if later you need to add some verification, you can, without breaking the API, i.e. ::

 private string _mystring; public string MyString { get {return _mystring;} set { if (IsAcceptableInput(value)) _mystring = value; } } 

Remember that a .NET property in .NET is just a cleaner syntax for two methods: one method for the get property section and one for the property set section. It provides all the same encapsulations as a couple of methods in C ++, but (possibly) a more convenient syntax to use.

+14
source share

Well, therefore, properties are not a wild west, to which you seem them at first glance. The unfortunate truth of OOP is that many of your methods are getters and setters, and properties are just a way to make this easier to write. You also control what you want the property to allow someone to do. You can make a property readable but not writable, for example:

 private string _mystring; public string MyString { get {return _mystring;} } 

Or, as Reed mentioned, you can use your set method to convert or test any complexity. For example, something like

 private long myPrime; public long Prime { get { return myPrime; } set { if (prime(value) { myPrime = prime; } else { //throw an error or do nothing } } } 

Typically, you have all the meaning you get from encapsulation, with some syntactic sugar, to simplify some common tasks. You can do the same as properties in other languages, it just looks different.

+2
source share

The advantage of the properties is that later you can decide to add validation, etc. to the setter method or to make the getter method to perform some calculations or caching, and none of the code that already calls your property will need to be changed - since the class interface remained stable

+1
source share

There is still data encapsulation if you need it. Encapsulating data is not about hiding it from the client of the class or making it inaccessible, but about ensuring a consistent interface and state of the internal object.

Let's say you have an object representing a stick changer and the property to set the speed. You probably know that you need to switch gears between speed intervals to where encapsulation takes place.

Instead of simply publicly accessing the resource, which allows public access without any verification, you can use the getters and seters properties in C #:

 class StickShiftCar : Car { public int MilesPerHour { get {return this._milesPerHour;} set { if (vaule < 20) this._gearPosition = 1; else if (value > 30) this._gearPosition = 2; ... ... this._milesPerHour = value; } } 

Although this example does not necessarily compile, I am sure you will catch my drift.

+1
source share

Perhaps you are missing the fact that you do not need to have properties that match all the fields of the class members. You can decide which properties to add to your class, and whether they will be available outside the class.

+1
source share

Looking a little deeper, why did your professor tell you to encapsulate? Just because it's the right object oriented design? Why is this so? Programming languages ​​and paradigms are just a way to deal with the complexity of getting a processor to run our code in an understandable way. There are two code readers, a machine and people. The machine will happily load data from any section in memory or separate it. We humans, on the other hand, think about "things." Our brains relate to “things” that have attributes or perform actions. The lion will eat you, the spear can protect you, the lion is fluffy, the spear is pointed. Therefore, we can understand programs if they are modeled as “things.” It is assumed that properties should model the attributes of a thing, and methods should model actions. In practice, this can become quite fuzzy, and everything cannot be modeled as an action of the real world, but trying to do it when everything is fine can make the program understandable.

0
source share

The very first attempt to use a property to encapsulate a value is get; set; But C # provides a more advanced function to enrich the functions inside get and set, to make the property read-only, write-only, or with certain conditions. For example, to set the value as

 private string temp; public string temp { get { return temp; } } 

will be better than using:

 public readonly string Temp; 
0
source share

All Articles