tags:

views:

133

answers:

5

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?

+2  A: 

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.

gehho
Is there a reason this is not possible in C#? Why do the creators not allow this?
it is really hard to ask questions here. I have to word it so carefully. to your edit: For example private is allowed for types within classes and everything within the class has access to them. private does not restrict ALL access to a type
I do not understand your comment. Is this a question? If so, please rephrase it or give more details. Or do you think my answer is wrong?
gehho
@gehho, erm, private does not restrict ALL access which is what you answer implied. private classes (in my little world :)) would not be inaccessible, just accessible within a namespace. But it seems I misunderstood what a namespace is
OK, that is right. **IF** the namespace was something comparable to a type, a private class could be accessed by every class within this namespace. But as we now know, a namespace is just some sort of identifier, and therefore private classes would not be accessible by anyone/anything. I think after you edited your question, I misunderstood its intention.
gehho
+1  A: 

In addition to gehho's answer: namespace is just a part of the type name (which only allows for all types to have unique names) - that's why you cannot restrict access to types inside a namespace.

Andrew Bezzub
So namespaces are only there to avoid conflict and not so much to group in a way a folder does? A namespace only gives context to it's containing types - is what you are saying?
@blacklion: Think of a namespace as a unique identifier for a type. It allows you to have 2 classes of the same name e.g. `System.DateTime` is different from `MyNamespace.DateTime`
James
Thanks, I think I was thinking of packages in java
+1  A: 

You can restrict the visibility to the current assembly by using an internal class.

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

Example

Define MyInternalClass in assembly AssemblyOne :

[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();
    }
}
Thibault Falise
Thanks, that was not my question though
@blacklion : Yeah, it does not restrict the visibility to the namespace. I misunderstood your question.
Thibault Falise
A: 

Because you can add types to namespace in any other external assembly. Would types in same namespace, but different assembly see your "private types"?

If no, we have difficult behaviour to understand (one type from namespace could see your type, another - no). If yes, we have a contradiction with definition of "private", because types in different assemblies would have an access to "private" details of assembly.

Aen Sidhe
see Thibault Falise answer concerning InternalsVisibleTo
A: 

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

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

If you want it as security feature, this will not work. If you want to declutter your namespace by hiding "internal" classes, you could have an Internals namespace underneath your main namespace, for example.

Daniel Rose