Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 971
Michael Avatar asked Feb 06 '12 09:02

Michael


3 Answers

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.

like image 190
phoog Avatar answered Nov 13 '22 07:11

phoog


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

like image 40
gdoron is supporting Monica Avatar answered Nov 13 '22 06:11

gdoron is supporting Monica


(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?

like image 8
Jon Skeet Avatar answered Nov 13 '22 05:11

Jon Skeet