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

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.


You can use only public or internal in the Namespace level


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
    }
}

Tags:

C#

.Net