I'm currently learning C# and I was wondering, what is the point of declaring classes / methods private? Who are we hiding / limiting access to these classes.
Because if someone was editing the source they could just change the tag from private to public. I'm not sure how a user will be able to access these methods and what problems it would cause.
tldr; What's the point of access modifiers.
Member visibility, as this feature is often called, is not a security feature. It is a convenience for the programmer, designed to help limit cross-class dependencies. By declaring a member private, you prevent other code from accessing it directly. This has two advantages:
Member visibility is probably the most important language feature in realizing encapsulation, one of the core principles of object-oriented programming.
This is a basic OO concept - encapsulation and has mostly nothing to do with security per se. To quote from Wikipedia (emphasis mine):
Hiding the internals of the object protects its integrity by preventing users from setting the internal data of the component into an invalid or inconsistent state. A benefit of encapsulation is that it can reduce system complexity, and thus increases robustness, by allowing the developer to limit the interdependencies between software components.
It keeps your code tidy. You separate your code into a public interface, and private internals.
That way, you can change your internals without fear of breaking code that depends on your class. You can also safely assume that no other code has modified your internal state while you weren't looking.
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