Question on c# Language Specification

Hello msdn community...

Reading ECMA-334 (http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf), on basic concepts (page 91) I came to the following two:

"Types declared in compilation units or namespaces can have public or internal declared accessibility and default to internal declared accessibility."

" Interface members implicitly have public declared accessibility. No access modifiers are allowed on interface member declarations."

My question is the following:

Declaring an interface at namespace without the use of any accessibility modifier will result in a interface with (default) internal access. How can this interface's members have public accessibility by default

efm01



Answer this question

Question on c# Language Specification

  • venup

    They have public accessibility to anything that can see the interface. Yes, in the instance you describe that is the same thing as internal accessibility, but that is not inconsistent; it just means that interface members effectively have the same level of accessibility as the interface itself, and there's no meaning or point in having accessibility modifiers on interface members.



  • MusicMan2006

    efm01 wrote:
    Mark, I would agree if this text was part of a common c# book. Since this comes from a specification, i trust a stricter verbalization, not needing interpretations, should have been used instead. For example your ..."interface members effectively have the same level of accessibility as the interface itself" or something like "interface members implicitly match container type's accessibility" would be better.

    The problem is that that's not true. Consider the following:



    public
    class MyClass
    {
    private class class1
    {
    private void method1() {}
    }

    private interface interface1
    {
    void method1();
    }

    public void Code(string[] args)
    {
    class1 cl1 =
    new class1();
    // cl1.method1(); // method1 is inaccessible

    interface1 int1 = null;
    int1.method1();
    }

    cl1.method1 is inaccessible (because it's private); int1.method1 is accessible, and therefore cannot be private. The method is public. Whether or not the class/interface itself is public is a completetly separate matter.



  • Mateusz Rajca

    Mark, I would agree if this text was part of a common c# book. Since this comes from a specification, i trust a stricter verbalization, not needing interpretations, should have been used instead. For example your ..."interface members effectively have the same level of accessibility as the interface itself" or something like "interface members implicitly match container type's accessibility" would be better.
  • Willy S

    Hi

    You need further read the ECMA. Chapter 20 starting on the page 343 gives beter explanation plus something like:

    All interface members implicitly have public access. It is a compile-time error for interface member

    declarations to include any modifiers. In particular, interface members cannot be declared with the modifiers

     abstract, public, protected, internal, private, virtual, override, or static



  • Question on c# Language Specification