Private classes inside namespaces

Possible duplicate:
Accessibility only for namespace in C # /. NET?

I want to have a class that is accessible only to other classes within the same namespace without the need to place the namespace in its own assembly.

Is there a reason this is not possible in C #?

Change I changed the question a bit since the compiler tells me that private not allowed. Can someone tell me the reason for this?

+7
c # oop
source share
5 answers

In addition to gehho, answer: namespace is just part of the type name (which allows only all types to have unique names) - therefore you cannot restrict access to types within the namespace.

+2
source share

It's impossible.

You can restrict access to the assembly containing the class using the internal modifier.

You can also restrict access to a class in one class by making the class a nested class. For example. if you make class B a private nested class in class A, only A will be available.

I think these two options are the best you can do. If you really want to restrict access to the namespace, you will have to put it in a separate assembly.

EDIT: To your second question: why the private class is not allowed. Private classes are only allowed if they are nested in another class. If you make a non-nested class private, what is its use? You still can’t access it, so you can’t do anything about it.

+5
source share

You can limit the visibility of the current assembly using the internal class.

However, you can allow another assembly to gain visibility for your assembly using the InternalsVisibleTo attribute.

Example

Define MyInternalClass in AssemblyOne assembly:

 [assembly: InternalsVisibleTo("AssemblyTwo")] internal class MyClass { internal void DoSomeAction() { } } 

Then define another class in AssemblyTwo:

 public class MyOtherClass { public void DoAnotherAction() { MyClass c = new MyClass(); c.DoSomeAction(); } } 
+2
source share

Because you can add types to the namespace in any other external assembly. Will there be types in the same namespace, but different assemblies see your "private types"?

If not, it’s hard for us to understand the behavior (one type from the namespace can see your type, the other not). If so, then we have a contradiction with the definition of "private", because types in different assemblies will have access to the "private" parts of the assembly.

0
source share

You can declare arbitrary namespaces so you can, for example, declare your own classes as part of the System namespace. Of course, this will result in an error if Microsoft decides to add a class with the same name to its namespace.

Why do you want to have "namespace visibility"? This would mean a special kind of audience, since anyone could declare their class as being in the required namespace and thus gain access to your classes.

If you want as a security feature this will not work. If you want to declare your namespace by hiding the "inner" classes, you can, for example, have an Internals namespace under your main namespace.

0
source share

All Articles