Possible Duplicate:
Namespace only class visibility in C#/.NET ?
What I want is to have a class that is only accessible to other classes inside the same namespace without having to put the namespace in it's own assembly.
Is there a reason this is not possible in C#?
Edit: I have change the question a little, since the compiler tells me that private
is not allowed. Can anyone tell me the reason for this?
This is not possible.
You can only restrict the access to the assembly containing the class using the internal
modifier.
You could also restrict the access to the class to a single class by making the class a nested class. E.g. if you make class B a private nested class in class A, B will only be accessible by A.
I think these two options are the best you can do. If you really want to restrict the access to the namespace, you will have to put it in a separate assembly.
EDIT: To your second question: why a private class is not allowed. Private classes are only allowed if they are nested within another class. If you make a non-nested class private, what is its use? You cannot access it anyway, so you cannot do anything with it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With