Luke
Luke

Reputation: 5971

When should you declare a class private or public?

I usualy declare my classes public. When does it make sense to declare a class private?

Upvotes: 2

Views: 3550

Answers (2)

Andrey
Andrey

Reputation: 60065

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.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500035

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.

Upvotes: 8

Related Questions