0

I have ClassLibrary project in C# and all my 'private classes' (under different namespace) are accessible to each other inside the same assembly (project).

Class1.cs
----------------------------
namespace MyClass1App
{
   private class Class1{}
}


Class2.cs
----------------------------
namespace MyClass2App
{
   private class Class2{}
}

Now Class1() can access and create instance of Class2() class [like... new MyClass2App.Class2() ]. and yes, these classes (Class1() and Class2()) are not accessible outside the assembly. Its the same behavior when these classes are made as 'Internal'. Can someone help me understanding whats the actual use/difference of 'private' and 'internal' access specifiers when applied on class level?

Thanks!

Pavan G R
  • 306
  • 3
  • 16

3 Answers3

3

You should not be able to declare a class as private at the namespace level. You can only have a private class if it is embedded within another class.

I get an error if I try to do this:

namespace MyApp
{
    private class Class1
    {
    }
}

This is the error message:

Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal

devuxer
  • 41,681
  • 47
  • 180
  • 292
  • You said it right! I mentioned 'private' in my code example just to make my question clear. You can compile the code removing 'private' keyword. in that case compiler still consider class1 as private. – Pavan G R Dec 30 '10 at 07:34
  • 1
    @Pavan, if you declare a class wiyhout any access modifiers, it is considered internal, not private. What you say might be true for nested classes (I don't remember what is the default access level for them), but certainly not for top-level classes. – VladV Dec 30 '10 at 07:39
  • @Will, actually `internal` is the default for a class declared at namespace level. – devuxer Dec 30 '10 at 07:44
3

alt text

For normal classes you can only apply public and internal other access modifiers don't make sense.

Nested classes can have all access modifiers types.

  • 1
    I ignore the Access Modifier table given above, because it only talks about class members and not really on class level. But I find your one liner reply useful and quickly answering my question. Thanks! – Pavan G R Dec 30 '10 at 08:19
1

Access Modifiers (C# Programming Guide)

Class or struct members can be declared with one of five types of access. They can be public or internal, like the classes and structs themselves. A class member can be declared as protected using the protected keyword, meaning that only derived types using the class as a base can access the member. By combining the protected and internal keywords, a class member can be marked protected internal — only derived types or types within the same assembly can access that member. Finally, a class or struct member can be declared as private with the private keyword, indicating that only the class or struct declaring the member is allowed access to that member.

Duplicate Question: Internal vs. Private Access Modifiers

Community
  • 1
  • 1
Theofanis Pantelides
  • 4,724
  • 7
  • 29
  • 49
  • when it comes to class/struct 'memebers' the access specifiers work pretty fine as said in C# Guide. However my question is on applying access modifiers on 'class' level and those classes are not a member of any other class. – Pavan G R Dec 30 '10 at 07:32
  • As long as you have different namespaces, I don't see why you should be experiencing different behavior – Theofanis Pantelides Dec 30 '10 at 07:35
  • I see your point, however, "private classes inside a namespace shouldn't be shown to the outside world" - isn't that make things more systematic? – Pavan G R Dec 30 '10 at 07:53