6

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.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Hexo
  • 215
  • 4
  • 11
  • 2
    You don't really "protect" them from anyone. Nobody uses access modifiers for security. Well, except perhaps some poor fools who got the idea completely wrong. There's nothing secure about it - one can modify the source code as you said; lacking source code one can tamper with the compiler output; lacking that, one can do the most devious things with reflection in many languages including C#. –  Feb 12 '12 at 00:24
  • 1
    http://www.merithub.com/q/99-encapsulation-oops.aspx – L.B Feb 12 '12 at 00:25
  • Believe me: Yourself! – Alexis Wilke Jan 11 '21 at 20:27

6 Answers6

7

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:

  • if you find that a member variable gets manipulated in a way you did not intend, the amount of code you have to check is significantly smaller when the variable is private
  • you can change the inner workings of a class (everything that is declared private) without breaking the interface (everything declared public)

Member visibility is probably the most important language feature in realizing encapsulation, one of the core principles of object-oriented programming.

tdammers
  • 20,353
  • 1
  • 39
  • 56
1

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.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
1

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.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
0

Access modifiers are used for encapsulation: they allow you to arrange your code in packages and classes, and have only an "official" public interface visible to the outside, while hiding the implementation details (which you want to do, so that you can later change it without telling anyone).

This is especially (only?) important, when you release code as a library for other programmers to use. Even if the code is only used in your own program, it helps to structure larger programs into several packages. - Thilo

Community
  • 1
  • 1
Cody Bonney
  • 1,648
  • 1
  • 10
  • 17
0

You can write code in 2 ways: designed for extension and designed for usage as it is.

You may or may not have access to source code.

Access modifiers are most important in code designed for extension where the client programmer (the programmer which extends and uses your code) needs to have access only to the API you expose - basically, to the interface you offer by using these access modifiers.

Bogdan T.
  • 668
  • 6
  • 13
0

Access modifiers are used for encapsulation, so that the internals of an object/class are hidden from outside users or classes. This prevents users from accidentally or intentionally breaking an object/class by modifying its instance variables and/or functions.

Encapsulation is an important part of the Open/closed principle, which refers to a class being "open for extension" but "closed for modification". This principle allows the extension of classes without fear that the API of an class may change in the future.

Kevin Ji
  • 10,479
  • 4
  • 40
  • 63