The C# compiler complains about the following code containing new protected member declared in struct
. What is the problem?
struct Foo {
protected Object _bar;
}
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.
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.
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.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With