dotnet / dotnet/csharpstandard

8.5.4 Protected access clarifications needed

Open
#71 4 comments 0 reactions 0 assignees View on GitHub
type: bug type: clarity
Dominant language
C#
Stars
815
Forks
99
Avg merge
1d 14h
Merged PRs (30d)
16

Description

Raised by: Peter Golde

Original comment on spreadsheet said "See Notes", but I could not find the notes. [Rex: See notes at end of this comment]

Raised by Peter Golde on February 13, 2003 originally on 10.5.3

@RexJaeschke said In E3 WD3.1, now 9.5.4.

1.2 Protected access clarifications needed

1.2.1 When is protected access rule enforced?

The specification isn't very clear about when the rules in 10.5.3 (protected access) are taken into account. In particular, are they taken into account during member lookup, which considers only "accessible members", or are they after member lookup has taken place. The specification is not very clear.

Here is an example where it makes a difference:

```
class A
{
public int m = 7;
}

class B: A
{
new protected string m = "hello";
}

class D: B
{
static void Main()
{
D d = new D();
B b = d;

Console.WriteLine(d.m);
Console.WriteLine(b.m); // prints 7 or compiler error
}
}
```

The Microsoft C# compiler compiles this and prints "hello" and "7", which we think is the correct behavior.

1.2.2 Access to protected instance constructor

Should a derive class be allowed to call an instance constructor from the base via new().

class B
{
protected B() {}
}

```
class D: B
{
public void f()
{
B b = new B(); //legal?
}
}
```

}

This should be seemingly disallowed by analogy to the other protected access rules, but the spec doesn’t call it out. The IL verifiers disallows this code so it should not be valid C#.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.