Scover
Scover

Reputation: 138

Protected structs in static classes

In C#, this :

static class foo
{
    protected void doThing(); // CS1057
    {

    }
}

Generates the CS1057 compiler error, as static classes don't support inheritance.

So why is this allowed? What is the meaning of the protected keyword in this context?

static class foo
{
    protected struct bar
    {

    }
}

Upvotes: 2

Views: 115

Answers (1)

Charlieface
Charlieface

Reputation: 72501

This is definitely a bug, and I would suggest you file it.


ECMA-334, the official specification for C#, says the following

15.2.2.4 Static classes
15.2.2.4.1 General

...snip...
A static class declaration is subject to the following restrictions:

  • ...snip...
  • A static class shall not have members with protected or protected internal declared accessibility.

Don't tell me that this excludes nested types, because a few lines later, nested types are specifically excluded from the static modifier restrictions:

The members of a static class are not automatically static, and the member declarations shall explicitly include a static modifier (except for constants and nested types).

Upvotes: 1

Related Questions