Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

private classes inside namespaces [duplicate]

Tags:

c#

oop

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?

like image 307
user318253 Avatar asked Apr 16 '10 08:04

user318253


1 Answers

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.

like image 104
gehho Avatar answered Oct 17 '22 21:10

gehho