What is the purpose of this approach for access modifiers?

Consider the following code:

private string _text = null; private string[] _values = null; public string Text { get { return _text; } } public string[] Values { get { return _values; } } 

What does it mean that there will be no public members only?

+7
source share
5 answers

Using properties instead of public fields, you hide the implementation.

If at some point you need to change the properties of the text and values, you can change the behavior without changing the API of this class.

In addition, this idiom restricts external access to open data as read-only.

+10
source

It should do readonly values, although I would be more inclined to write it like this:

 public string Text { get; private set;} public string[] Values { get; private set; } 
+6
source

This code will allow external entities to read your values, while your internal code can change the support field.

You can use this function with auto-properties:

 public string Text { get; private set; } public string[] Values { get; private set; } 

With an open field, you cannot protect against external changes and support internal modification at the same time.

Even if you want to open the field as external for writing, I would still suggest encapsulating the thing in the property - you never know if you need to support something internally without breaking the external contract (the user code in set / get gives you to do this).

Not to mention the fact that most data binding structures do not see fields, but only properties.

+5
source

_text and _values can only be set within the class that they are defined, but their values ​​can be accessed through properties.

+2
source

In your example, this makes the properties read-only, but there are other uses.

 public string Text { get { return _text; } } 

If you want to perform some operation inside return_text and then return it against proeperty Text , you could be something like.

 public string Text { get { return _text.ToUpper(); } } 

This field is Encapsulation

Encapsulation is sometimes referred to as the first column or principle of object-oriented programming. According to the encapsulation principle, a class or structure can indicate how accessible each of its members should encode outside the class or structure. Methods and variables that are not intended to be used outside the class or the assembly can be hidden to limit the potential for coding errors or malicious exploits.

Consider the following example:

 // private field private DateTime date; // Public property exposes date field safely. public DateTime Date { get { return date; } set { // Set some reasonable boundaries for likely birth dates. if (value.Year > 1900 && value.Year <= DateTime.Today.Year) { date = value; } else throw new ArgumentOutOfRangeException(); } } 

In this example, there is a private date field that is openly open through the date property. Now, if you want to set the border for the date, you can see the set part of the property.

+2
source

All Articles