Namespace elements cannot be explicitly declared as private, protected, or protected internal

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • newbie120@gmail.com

    Namespace elements cannot be explicitly declared as private, protected, or protected internal

    Hi all

    maybe its just been a long day, but i have a question about call access
    modifiers in C#. Consider the following code.

    namespace Application
    {
    private class Class1
    {
    int i;
    }
    }

    When I try to compile this i get the following error:
    "Namespace elements cannot be explicitly declared as private,
    protected, or protected internal"

    When i make Class1 public i no longer get a compile error. Now correct
    me if i'm wrong, but doesn't C# allow me to have private or protected
    class structures? Am i missing something?

  • Joanna Carter [TeamB]

    #2
    Re: Namespace elements cannot be explicitly declared as private, protected, or protected internal

    <newbie120@gmai l.com> a écrit dans le message de news:
    1150753477.4551 79.22180@h76g20 00...legro ups.com...

    Answered in dotnet.general.

    Joanna

    --
    Joanna Carter [TeamB]
    Consultant Software Engineer


    Comment

    • Peter Bromberg [C# MVP]

      #3
      RE: Namespace elements cannot be explicitly declared as private, prote

      From MSDN documentation:

      Namespace elements cannot be explicitly declared as private, protected, or
      protected internal

      Type declarations in a namespace can have either public or internal access.
      If no accessibility is specified, internal is the default.

      The following sample generates CS1527:

      // CS1527.cs
      namespace Sample
      {
      private class C1 {}; // CS1527
      protected class C2 {}; // CS1527
      protected internal class C3 {}; // CS1527
      }

      --
      Co-founder, Eggheadcafe.com developer portal:

      UnBlog:





      "newbie120@gmai l.com" wrote:
      [color=blue]
      > Hi all
      >
      > maybe its just been a long day, but i have a question about call access
      > modifiers in C#. Consider the following code.
      >
      > namespace Application
      > {
      > private class Class1
      > {
      > int i;
      > }
      > }
      >
      > When I try to compile this i get the following error:
      > "Namespace elements cannot be explicitly declared as private,
      > protected, or protected internal"
      >
      > When i make Class1 public i no longer get a compile error. Now correct
      > me if i'm wrong, but doesn't C# allow me to have private or protected
      > class structures? Am i missing something?
      >
      >[/color]

      Comment

      • Chris Jobson

        #4
        Re: Namespace elements cannot be explicitly declared as private, protected, or protected internal

        <newbie120@gmai l.com> wrote in message
        news:1150753477 .455179.22180@h 76g2000cwa.goog legroups.com...[color=blue]
        > Hi all
        >
        > maybe its just been a long day, but i have a question about call access
        > modifiers in C#. Consider the following code.
        >
        > namespace Application
        > {
        > private class Class1
        > {
        > int i;
        > }
        > }
        >
        > When I try to compile this i get the following error:
        > "Namespace elements cannot be explicitly declared as private,
        > protected, or protected internal"
        >
        > When i make Class1 public i no longer get a compile error. Now correct
        > me if i'm wrong, but doesn't C# allow me to have private or protected
        > class structures? Am i missing something?[/color]

        The definition of private is "Private members are accessible only within the
        body of the class or the struct in which they are declared", so I guess it
        makes no sense to declare something private if it's not within a class or
        struct. Similarly protected only makes sense for a class member (but not a
        struct member, since a struct cannot inherit from another struct). Thus I
        think you can have private or protected classes, but only if they are nested
        within another class/struct.

        I also found this in the help: "Classes and structs that are not nested
        within other classes or structs can be either public or internal. A type
        declared as public is accessible by any other type. A type declared as
        internal is only accessible by types within the same assembly. Classes and
        structs are declared as internal by default unless the keyword public is
        added to the class definition".

        Chris Jobson


        Comment

        • Chris Jobson

          #5
          Re: Namespace elements cannot be explicitly declared as private, protected, or protected internal

          <newbie120@gmai l.com> wrote in message
          news:1150753477 .455179.22180@h 76g2000cwa.goog legroups.com...[color=blue]
          > Hi all
          >
          > maybe its just been a long day, but i have a question about call access
          > modifiers in C#. Consider the following code.
          >
          > namespace Application
          > {
          > private class Class1
          > {
          > int i;
          > }
          > }
          >
          > When I try to compile this i get the following error:
          > "Namespace elements cannot be explicitly declared as private,
          > protected, or protected internal"
          >
          > When i make Class1 public i no longer get a compile error. Now correct
          > me if i'm wrong, but doesn't C# allow me to have private or protected
          > class structures? Am i missing something?[/color]

          The definition of private is "Private members are accessible only within the
          body of the class or the struct in which they are declared", so I guess it
          makes no sense to declare something private if it's not within a class or
          struct. Similarly protected only makes sense for a class member (but not a
          struct member, since a struct cannot inherit from another struct). Thus I
          think you can have private or protected classes, but only if they are nested
          within another class/struct.

          I also found this in the help: "Classes and structs that are not nested
          within other classes or structs can be either public or internal. A type
          declared as public is accessible by any other type. A type declared as
          internal is only accessible by types within the same assembly. Classes and
          structs are declared as internal by default unless the keyword public is
          added to the class definition".

          Chris Jobson


          Comment

          Working...