none
Confused over Internal access modifier, error if never specified RRS feed

  • Question

  • Hi,

    I read on the microsoft article that :-

    Internal is the default if no access modifier is specified. Struct members, including nested classes and structs, can be declared as public, internal, or private. Class members, including nested classes and structs, can be public, protected internal, protected, internal, private protected or private.

    However, I tried the same with the following code

    class Cally2
        {
            static string callyname;
        }

    and I tried setting a value for the variable callyname, however, I get the following error :-

    Cally2.callyname' is inaccessible due to its protection level

    The error disappears when I add the internal keyword like this

     internal class Cally2
        {
            internal static string callyname;
        }
    I am kinda confused with the statement in the article that says, if no access modifier's are specified, then it can be assumed that it defaults to internal. Why then must I specify the internal access modifier?

    Thursday, October 18, 2018 9:40 AM

Answers

  • Maybe because “the access level for class members and struct members, including nested classes and structs, is private by default”.

    Internal is default for Cally2, private is default for callyname.

    • Edited by Viorel_MVP Thursday, October 18, 2018 10:36 AM
    • Marked as answer by cally_kalpana Thursday, October 18, 2018 4:30 PM
    Thursday, October 18, 2018 10:34 AM
  • Hi,

    default class access modifier is internal, but default member modifier is private. But this is just what Viorel_ said, too.

    Greeting, Chris


    • Edited by DerChris88 Thursday, October 18, 2018 11:14 AM
    • Marked as answer by cally_kalpana Thursday, October 18, 2018 4:30 PM
    Thursday, October 18, 2018 11:12 AM
  • As others have mentioned the default accessibility for a root (non-nested) type is different than for members. To be clear, the default is private, not internal. So everything is only accessible within the type that defines it.

    The problem with a root type is that it cannot be private. Private only makes sense within the context of a type. Root types can only be public or internal. Since a root type is not contained in another type having it private doesn't make any sense. The default accessibility of private isn't valid for a root type so the language steps back and uses the next least-restrictive access which is internal. Hence root types are internal by default, because private isn't valid.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by cally_kalpana Thursday, October 18, 2018 4:30 PM
    Thursday, October 18, 2018 2:50 PM
    Moderator

All replies

  • Maybe because “the access level for class members and struct members, including nested classes and structs, is private by default”.

    Internal is default for Cally2, private is default for callyname.

    • Edited by Viorel_MVP Thursday, October 18, 2018 10:36 AM
    • Marked as answer by cally_kalpana Thursday, October 18, 2018 4:30 PM
    Thursday, October 18, 2018 10:34 AM
  • Hi,

    default class access modifier is internal, but default member modifier is private. But this is just what Viorel_ said, too.

    Greeting, Chris


    • Edited by DerChris88 Thursday, October 18, 2018 11:14 AM
    • Marked as answer by cally_kalpana Thursday, October 18, 2018 4:30 PM
    Thursday, October 18, 2018 11:12 AM
  • As others have mentioned the default accessibility for a root (non-nested) type is different than for members. To be clear, the default is private, not internal. So everything is only accessible within the type that defines it.

    The problem with a root type is that it cannot be private. Private only makes sense within the context of a type. Root types can only be public or internal. Since a root type is not contained in another type having it private doesn't make any sense. The default accessibility of private isn't valid for a root type so the language steps back and uses the next least-restrictive access which is internal. Hence root types are internal by default, because private isn't valid.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by cally_kalpana Thursday, October 18, 2018 4:30 PM
    Thursday, October 18, 2018 2:50 PM
    Moderator
  • Thank you...for the explanation. Helps alot for a beginner like me.
    Thursday, October 18, 2018 4:30 PM