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 this accomplish that having the public members alone would not?
The purpose of using access modifiers is to implement encapsulation, which separates the interface of a type from its implementation.
Simply put, there are four access modifiers: public, private, protected and default (no keyword).
C) Access modifiers help in implementing the Encapsulation feature of Object-Oriented Programming (OOPs).
Access modifiers are used to implement an important aspect of Object-Oriented Programming known as Data Hiding.
By using properties instead of public fields, you hide the implementation.
If at some point you need to change what the Text and Values properties return, you can change the behavior without changing the API of the class.
Additionally, this idiom limits external access to the exposed data as read-only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With