When should you declare a class private or public?
I u开发者_Python百科sualy declare my classes public. When does it make sense to declare a class private?
You can only declare a class as private when it's nested within another class. Top-level classes can be made internal, however.
You'd hide a class from the outside world when it's meant to be an implementation detail rather than providing an API everyone can use. The downside is that the outside world can't use that code directly even if it would be useful to them - the upside is that you can change the class however you like in the future without breaking the rest of the world, so long as your public API remains the same in both shape and behaviour.
You can't declare class private unless it is nested class, if it is not it is internal. Why would you do this? Because this class is used internally in assembly and not needed by client.
精彩评论