16

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?

Community
  • 1
  • 1
user318253
  • 163
  • 1
  • 1
  • 6
  • is there anything that has not been asked? Missed this when I wrote the question. But any insights into why this is so? – user318253 Apr 16 '10 at 08:26
  • hehe... I am wondering how people find duplicate posts that fast although it is not even listed in the "Related" section on the right. @James: How do you do that? :) – gehho Apr 16 '10 at 08:29
  • Maybe if you say why you need to do that people can offer you alternatives? As it stands what you want to do is impossible to achieve. – Phil Gan Apr 16 '10 at 09:00
  • @Phil, I understand the alternatives. I just wanted to understand the logic. Maybe this is not the right place to ask this question. For example: you have a "everything is virtual unless it is final" type of thing in java while in C# you have to mark something as virtual. I understand why that is and I just wanted to understand why you don't have "package private" kind of thing in C#. It's more a "why" than a "how" question. – user318253 Apr 16 '10 at 09:20
  • @blacklion: I understand. It almost feels like what you want to do is 'wrong' because I'm so used to the way things are. Although it does seem there would be valid cases for limiting the reusability of certain classes I can imagine code structure quickly getting quite complicated. Maybe it's just a case of KISS? – Phil Gan Apr 16 '10 at 10:39
  • @Phil, technically it is already done if I put it in an assembly. Glad I asked the question cause I have a better idea of namespaces and assemblies. Still have lots of questions though – user318253 Apr 16 '10 at 11:53

5 Answers5

10

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
  • 9,049
  • 3
  • 45
  • 59
  • Is there a reason this is not possible in C#? Why do the creators not allow this? – user318253 Apr 16 '10 at 08:33
  • 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 – user318253 Apr 16 '10 at 08:54
  • 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 Apr 16 '10 at 09:18
  • 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 – user318253 Apr 16 '10 at 09:24
  • 1
    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 Apr 16 '10 at 10:18
4

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
  • 5,795
  • 2
  • 29
  • 32
3

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
  • 15,744
  • 7
  • 51
  • 73
  • 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? – user318253 Apr 16 '10 at 08:51
  • @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 Apr 16 '10 at 08:56
  • Thanks, I think I was thinking of packages in java – user318253 Apr 16 '10 at 09:07
0

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
  • 1,181
  • 12
  • 25
0

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
  • 17,233
  • 9
  • 65
  • 88