The error "Elements defined in the namespace cannot be explicitly declared as private, protected or protected internal"

I tried to make the class private and got this error . Elements defined in the namespace cannot be explicitly declared as private, protected, or protected internal "

I have a point, but I want to ask why this is not allowed? Are all access modifications not applicable to the class? Why can't I make a class private, protected, or protected internally?

+56
c #
Sep 11 '11 at 16:39
source share
9 answers

Since private means that the element is only available in the containing class. Since a top-level class does not have a class containing it, it cannot be private (or protected). (However, internal or public valid modifiers).

What would you like to keep in mind for a top-level class?

Of course, all modifiers apply to nested classes, that is, to a class defined in another class.

+73
Sep 11 '11 at 16:41
source share
— -

You can only use public or internal at the namespace level

+13
May 01 '13 at 17:29
source share

As Abatonime said , you can only use public or internal at the namespace level.
private , protected or protected internal can only be used at the class level.

It works

 namespace X { class A { // class code here private class B { // class code here } } } 

It will not be

 namespace X { class A { // class code here } private class B { // class code here } } 
+4
Sep 20 '15 at 19:09
source share

Because it does not make sense. There is no way to access protected or private classes defined at the namespace level only as nested classes.

+3
Sep 11 2018-11-11T00:
source share

Only nested classes can be declared as private. Non-nested classes can only be public or internal (implicit without modifiers)

+2
Sep 11 '11 at 16:42
source share

I had the same problem because I was creating a custom DLL and wanted certain classes to be visible to the application using the DLL. So I just delete the modifier completely for classes that I would like to be private (within specific namespaces). Classes remained available to other classes within the same namespace in the DLL, but did not appear in Intellisense in the calling application. No need for nested classes. The only explanation I can think of is an error message that cannot "explicitly" declare private ... it says nothing about implicitly.

 namespace SmartCardAuthentication { class SmartCardIdentity : IIdentity { private string _firstName; private string _lastName; private string _middleInitial; .... } } 

In the above example, the class code "SmartCardIdentity" is available for another class in the same namespace, but is not available to the calling application when this class is loaded into the DLL. I have not tested it yet (e.g. visibility from a class in a different namespace in a DLL.).

+1
May 19 '16 at 15:52
source share

The default accessibility of top-level types is internal .

The accessibility of class and structure elements is private by default.

The only possible availability of interface elements and enumerations is public .

So, the default class is private , and if you want to access it, you need to put public before this.

0
Jun 23 '14 at 6:55
source share

When defining a class, only public and internal are applicable. If the access modifier is not set before its default value is internal.

refer to MSDN - [ https://msdn.microsoft.com/en-us/library/8fd16xs0(v=vs.90).aspx]

0
Sep 24 '15 at 14:12
source share

In the real world, we focus on the visible object. As soon as the object is visible, we are talking about the scope of the object.

example in the real world If you walk along the street, you see houses, there are houses in the colony. If the colony is protected, no one can see at home. It is believed that there are no houses in the colony.

In programming If we make a class private / protected at the top level, no one knows about it, is it present in the assembly?

correct me please if I'm out of sight

0
May 03 '19 at 17:37
source share



All Articles