weberc2
weberc2

Reputation: 7908

New protected member declared in struct

The C# compiler complains about the following code containing new protected member declared in struct. What is the problem?

struct Foo {
    protected Object _bar;
}

Upvotes: 8

Views: 5638

Answers (3)

someone
someone

Reputation: 101

From the MSDN docs:

A struct cannot be abstract and is always implicitly sealed.

It looks like C# wants you to use "private" instead of protected.

Upvotes: 10

Sergey Teplyakov
Sergey Teplyakov

Reputation: 11647

Structs are implicitly sealed so you can't create descendants any way and protected modifier means that only instance of this type and all instances of derived types has access to it.

Upvotes: 2

weberc2
weberc2

Reputation: 7908

Because this is a struct, it cannot be overridden. It seems the C# compiler wants sealed types like structs to use the 'private' keyword rather than the 'protected' keyword, even though functionally there isn't any difference. Use this instead:

struct Foo {
    private Object _bar;
}

Upvotes: 3

Related Questions