Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New protected member declared in struct

Tags:

c#

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

struct Foo {
    protected Object _bar;
}
like image 714
weberc2 Avatar asked Dec 03 '12 19:12

weberc2


People also ask

Can struct have protected members?

Struct members can't be declared as protected , protected internal , or private protected because structs don't support inheritance. Normally, the accessibility of a member isn't greater than the accessibility of the type that contains it.

How do you inherit protected members in C#?

A derived class has access to the public, protected, internal, and protected internal members of a base class. Even though a derived class inherits the private members of a base class, it cannot access those members.

What is the protected keyword in C#?

Protected is a keyword that C# uses to make access restriction for class members. When we mark members as protected, it becomes accessible only in the class where it's defined or inside the derived class. The protected keyword is used to share functionality that derived classes might find useful.

What is the default value of struct in C#?

For variables of class types and other reference types, this default value is null . However, since structs are value types that cannot be null , the default value of a struct is the value produced by setting all value type fields to their default value and all reference type fields to null .


1 Answers

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.

like image 51
someone Avatar answered Sep 27 '22 22:09

someone