Why is my use of "protected" work not used?

I read that access to the protected member can be obtained from derived classes, but the following does not work.

class A { protected int Test; } class B:A { A instanceOfA= new A() public B() { instanceOfA.Test //Not possible } } 
+4
source share
7 answers

I read that a protected member can be accessed from derived classes. Why is my use of “protected” not working?

This is illegal because you have not made a guarantee that you are accessing instance B data. Consider this similar case:

 abstract class BankAccount { protected int accountNumber; } class SwissBankAccount : BankAccount { } --- in another assembly, evil-doers write --- class EvilBankAccount : BankAccount { void DoEvil() { BankAccount b = GetASwissBankAccount(); int number = b.accountNumber; } } 

EvilBankAccount does not inherit from SwissBankAccount, therefore a protected member of SwissBankAccount is not allowed to be used inside EvilBankAccount. You can access the protected members of your "parents", but not your "brothers and sisters"! EvilBankAccount can only access protected members of EvilBankAccount (or a type obtained from EvilBankAccount). SwissBankAccount protected members have no restrictions.

The rule is that the type of “recipient” expression that accesses a protected instance must be at least the same as the type declaration containing member access. For a precise statement of the rule and some illustrative examples, see Section 3.5.3 of the C # 4.0 Specification.

By the way, C ++ also has this rule.

This rule is often misunderstood. For a more detailed analysis of this rule and some other consequences of secure access, see my articles on this subject. The most relevant articles I wrote on this subject are this one and this one . There are a bunch of more articles I wrote on related topics here (although some of them will go over the topic of secure access itself and on how to use secure access to create a data model with parent links.)

+26
source

You have configured your code incorrectly. Class B should not have an instance of class A Instead, class B itself inherits the protected variables from class A

Your code should look bigger:

 class A { protected int Test; } class B:A { public B() { int someInt = this.Test; } } 
+2
source

You can access Test int inside class B . However, you cannot access instance properties. A does not know that it is a child of B , so it will not provide access to its property.

 class A { protected int Test; } class B : A { public B() { Test = 3; //possible base.Test = 3; //explicitly calling base member, but not necessary in this case } } 
+2
source

Subclasses can access their own inherited members that are marked as protected .

 class A { protected int Test; } class B : A { public B() { this.Test = 42; // Possible } } 
+2
source

Since B already inheriting from A , you do not need a separate instance of A

  public B() { this.Test = 1; //possible } 
+2
source

You can access it through the same class as the public member of the base class.

 class A { protected int Test; } class B:A { void TestMethod() { this.Test = 3; // Possible } } 

Tick protected access modifier in C #.

+2
source

You do not need to instantiate A if you inherit A

 class A { protected int Test; } class B:A { public B() { this.Test = 666; } } 
+2
source

Source: https://habr.com/ru/post/1347757/


All Articles