Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data encapsulation in C# using properties

currently I am thinking about data encapsulation in C# and I am a little bit confused. Years ago, when I started to learn programing with C++, my professor told me: - "Create a class and hide it data members, so it can't be manipulated directly from outside"

Example: You are parsing an XML file and store the parsed data into some data members inside the parser class.

Now, when I am looking at C#. You have there properties. This feature makes the internal state / internal data of a class visible to outside. There is no encapsulation anymore. Right?

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

From my point of view there is no difference between making data members public or having public properties, which have getters and setters, where you pass your private data members through.

Can someone explaing me that please?

Thanks

like image 236
Ferhat Avatar asked Sep 17 '10 22:09

Ferhat


People also ask

Is there encapsulation in C?

It is one of the core features of Object-oriented languages. However, it is not only limited to OOP languages only. In C, encapsulation has been despite the absence of private and public keywords. Encapsulation is being used by various other programming languages like C#, C++, PHP, JAVA as well.

What is the data encapsulation?

Data encapsulation, also known as data hiding, is the mechanism whereby the implementation details of a class are kept hidden from the user.

What is an example of data encapsulation?

Data Encapsulation is an Object Oriented Programming concept that bind a group of related properties, functions, and other members are treated as a single unit. Class is the best example of Data Encapsulation. It sometimes referred to as data hiding that prevents the user to access the implementation details.

How is encapsulation achieved in C?

The process of implementing encapsulation can be sub-divided into two steps: The data members should be labeled as private using the private access specifiers. The member function which manipulates the data members should be labeled as public using the public access specifier.


1 Answers

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

In your situation above, there is little reason to use the property. However, if you later need to add some validation, you can, without breaking the API, ie::

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

Remember that a Property, in .NET, is really just a cleaner syntax for 2 methods - one method for the property get section, and one for the property set section. It provides all of the same encapsulation as a pair of methods in C++, but a (arguably) nicer syntax for usage.

like image 126
Reed Copsey Avatar answered Sep 29 '22 10:09

Reed Copsey