-4

Apart from the code being cleaner or to maintain the object-oriented status of c#, is there really any point in using private / protected instead of public?

vxern
  • 3
  • 3
  • 2
    This will likely get closed, as it's a duplicate of an already-closed question. My opinion: aside from security-related code, the main reason for encapsulation is that you can't trust other programmers (or your future self). People do foolish things in the name of expediency. Encapsulation acts as guardrails. (Update: https://softwareengineering.stackexchange.com/questions/143736/why-do-we-need-private-variables answers it much better.) – TrueWill Dec 22 '18 at 22:45

1 Answers1

5

Imagine you're the planner of a concert. The venue has a stage area and an audience area.

There are two ways to organize your concert. The first way is to allow anyone to enter or leave the audience area or the stage area from any direction.

The second way is to control the area by incorporating barriers and entrances, preventing anyone without a ticket or security check from entering, and allowing only authorized people to enter the stage area.

Which approach is better? Why?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501