Michael
Michael

Reputation: 13616

Why elements defined in a namespace cannot be explicitly declared?

I have the following C# code:

namespace ISeeOptic.BL
{

  public abstract class Process
  {        
     ...      

     protected static void DeleteImages(List<ImagesPath> list)
      {
          some logic
      } 

      ...
   }


    protected class GetDataBL: Process
    {
      ...

     public static void DeleteImages(List<ImagesPath> list)
     {
         DeleteImages(list); 
     } 
     ...
 }
} 

At compile-time I get the following Error:

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

I'm beginner in C# so maybe this question may seem naive, any idea what cause to this error?

Thank you advance.

Upvotes: 52

Views: 42628

Answers (5)

Afazal
Afazal

Reputation: 542

In class Private, Protected and Protected Internal access Specifiers are not allowed at namespace level.

Only allowed Specifiers are public and internal in class.

Only to the child classes private, protected or protected internal access Specifiers are allowed .

Sample code

internal class ParentClass
{
    public string test()
    {
        return "This is the parent class function";
    }
    private class BaseChildClass
    {
        protected string childtest()
        {
            return "This is the parent class function";
        }
    }

    private class DerivedChildClass : BaseChildClass
    {
        private void test1()
        {
            string test = base.childtest();

        }
    }
}

Upvotes: 4

Grant Thomas
Grant Thomas

Reputation: 45083

It's the scoping of the elements that is causing the error, as explained by the error - and the C# specification (ECMA section 10.5.1):

  • Types declared in compilation units or namespaces can have public or internal declared accessibility and default to internal declared accessibility.
  • Class members can have any of the five kinds of declared accessibility and default to private declared accessibility.
  • Struct members can have public, internal, or private declared accessibility and default to private declared accessibility because structs are implicitly sealed.

Upvotes: 3

gdoron
gdoron

Reputation: 150243

private protected means they will be accessible to this class or to the derived classes.
In the Namespace level there is no class to derived from so it useless.

You can use only public or internal in the Namespace level

MSDN docs

Upvotes: 19

Jon Skeet
Jon Skeet

Reputation: 1499770

(I believe you'll actually get a compile-time error; if you're only seeing this at execution time, then chances are your code is being compiled at execution time too, e.g. as part of a web app. Logically it's a compile-time error, not an exception.)

The protected access modifier (loosely) makes a member accessible to a derived containing type; but in the case of a namespace member there is no containing type.

Likewise a private member's accessibility domain is the program text of the containing type - and again, there is no containing type.

What are you actually trying to achieve by making GetDataBL protected?

Upvotes: 8

phoog
phoog

Reputation: 43036

Elements defined in a namespace may be explicitly declared public or internal.

They may not be explicitly declared private or protected (or protected internal) because these modifiers only make sense for members of a class.

Your protected class GetDataBL, for example, makes no sense, because "protected" means "accessible to classes that inherit from the containing class" -- but there is no containing class for GetDataBL.

Upvotes: 57

Related Questions