Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I access C# protected members except like this?

This code:

abstract class C {     protected abstract void F(D d); }  class D : C {     protected override void F(D d) { }      void G(C c)     {         c.F(this);     } } 

Generates this error:

Cannot access protected member 'C.F(D)' via a qualifier of type 'C'; the qualifier must be of type 'D' (or derived from it)

What in the world were they thinking? (Would altering that rule break something?) And is there a way around that aside from making F public?


Edit: I now get the reason for why this is (Thanks Greg) but I'm still a bit perplexed as to the rational; given:

class E : C {     protected override void F(D d) { } }   

Why shouldn't D be able to be able to call E.F?


The error message is edited so I might have put a typo in there.

like image 822
BCS Avatar asked Feb 19 '09 23:02

BCS


People also ask

Why do I not have access to my C drive?

Right click on Drive C and click Properties. b. Switch to Security tab, click Edit and Add to add your user account, if it is not listed. Assign the full control permission for your own account and click OK.

How do I regain access to C drive?

Try this: Right click Start button to open Run box, copy and paste in netplwiz, press Enter. Highlight your account, then click Properties, then Group Membership tab. click on Administrator, then Apply, OK, restart PC.

How do I access C Windows?

Your file explorer should appear by default on your task bar; it's icon looks like a file folder. If you don't have access to that shortcut, you can just type "this pc" or "file explorer" into the search box, and to get to your C: drive, just type "c:" into the same box.


1 Answers

The reason this doesn't work is because C# doesn't allow cross-hierarchy calling of protected methods. Say there was a class E that also derived from C:

  C  / \ D   E 

Then the reference you're trying to call the method on could actually be an instance of type E and thus the method could resolve at runtime to E.F. This is not permitted in C# as D cannot call E's protected methods, because E is in another branch of the hierarchy, i.e.

var d = new D(); var e = new E(); d.G(e); // oops, now this will call E.F which isn't allowed from D 

This makes sense because the keyword protected means the member "is accessible within its class and by derived class instances" and E.F is not a member of D.

like image 91
Greg Beech Avatar answered Oct 23 '22 22:10

Greg Beech