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

C#.Net

C# Problem Overview


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

I got its meaning but I want to ask why this is not allowed? Are all access modifires not applicable on Class? Why I can't make a class private, protected or protected internal?

C# Solutions


Solution 1 - C#

Because private means that the member is only visible in the containing class. Since a top-level class has no class containing it it cannot be private (or protected). (Internal or public are valid modifiers though).

What would you want private to mean on a top-level class?

Of course all modifiers apply to nested classes, i.e. a class defined within another class.

Solution 2 - C#

You can use only public or internal in the Namespace level

Solution 3 - C#

As Abatonime said, you can only use public or internal in the Namespace level.
private, protected, or protected internal can only be used in the Class level.

This works

namespace X
{
    class A
    {
        // class code here
        
        private class B // this class is an inner class
        {
            // class code here
        }
    }
}

This won't

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

    private class B // this is a class inside a namespace
    {
        // class code here
    }
}

Solution 4 - C#

Because it doesn't make sense. There's no way you can access protected or private classes defined at namespace level, only as nested classes.

Solution 5 - C#

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

Solution 6 - C#

I had this same problem because I was creating a custom DLL and only wanted certain classes to be visible to an application using the DLL. So I just remove the modifier completely for classes I wanted to be private (within specific namespaces). The classes remained accessible to other classes within the same namespace in the DLL but did not show up in Intellisense in the calling application. No need for nested classes. The only explanation I can think of is the error message says cannot "explicitly" declare private...it doesn't say anything about implicitly.

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

        ....
   }
}

In example code above, class "SmartCardIdentity" is available to other class within same namespace, but not available to calling application when this class is rolled into a DLL. I have not tested it anyother way (i.e. visibility from a class in a different namespace within the DLL.).

Solution 7 - C#

The default accessibility of top-level types is internal.

The default accessibility of class and struct members is private.

The only possible accessibility of interface and enum members is public.

So a class is by default private, and if you want to access that, you have to put public before that.

Solution 8 - C#

Only Public and Internal are applicable when defining class. If no access modifier is defined before the class default is internal.

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

Solution 9 - C#

In real world we are focus on visible object Once object is visible then we talk about scope of the object

example in real world

If you walking on street, you see houses in a colony colony has houses. If colony is protected no one can't able to see houses It is consider that no colony no houses is present

In Programming

If we make class as private/ protected at top-level no one known about it

is it present in assembly ?

please correct me, if i am out of the scope

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser576510View Question on Stackoverflow
Solution 1 - C#DeCafView Answer on Stackoverflow
Solution 2 - C#JohnView Answer on Stackoverflow
Solution 3 - C#SNagView Answer on Stackoverflow
Solution 4 - C#KostassoidView Answer on Stackoverflow
Solution 5 - C#VladimirView Answer on Stackoverflow
Solution 6 - C#dars67View Answer on Stackoverflow
Solution 7 - C#HemantView Answer on Stackoverflow
Solution 8 - C#Neeraj KumarView Answer on Stackoverflow
Solution 9 - C#Sameer BahadView Answer on Stackoverflow