I have two objects, RoomManager and Room, there will be several Rooms and one RoomManager. I want the RoomManager to be the only one allowed to create a Room object. So I’m wondering if there is a way to make the Room constructor (and the rest of the Room methods/properties) only accessible to the RoomManager. I was thinking maybe moving them to their own namespace and making Room private or internal or something. From Accessibility Levels (C# Reference) I see that internal is for the entire assembly though, not just the namespace.
I have two objects, RoomManager and Room , there will be several Room s
Share
No, C# (and .NET in general) has no access modifiers which are specific to namespaces.
One fairly hack solution would be to make Room just have a private constructor, and make RoomManager a nested class (possibly just called Manager):
Use it like this:
As I say, that’s a bit hacky though. You could put Room and RoomManager in their own assembly, of course.