I'm learning C# and coming from a Java world, I was a little confused to see that C# doesn't have a "package private". Most comments I've seen regarding this amount to "You cannot do it; the language wasn't designed this way". I also saw some workarounds that involve internal
and partial
along with comments that said these workarounds go against the language's design.
Why was C# designed this way? Also, how would I do something like the following: I have a Product
class and a ProductInstance
class. The only way I want a ProductInstance
to be created is via a factory method in the Product
class. In Java, I would put ProductInstance
in the same package as Product
, but make its constructor package private
so that only Product
would have access to it. This way, anyone who wants to create a ProductInstance
can only do so via the factory method in the Product
class. How would I accomplish the same thing in C#?
internal
where it is intended to be used only by a few "friendly" classes (see thefriend
keyword in C++). I don't want to break my application into hundreds of dll just to prevent smelly access, nor do I like to leave my detail implementation open (basically public to the whole dev team). Should I write giant classes with ten subclasses to overcome this limitation? – Lyudmila