Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 147901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:51:46+00:00 2026-05-11T08:51:46+00:00

This code: abstract class C { protected abstract void F(D d); } class D

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:51:47+00:00Added an answer on May 11, 2026 at 8:51 am

    The ‘protected’ keyword means that only a type and types that derive from that type can access the member. D has no relationship to C therefore cannot access the member.

    You have a couple of options if you want to be able to access that member

    • Make it public
    • Make it internal. This will allow any types to access the member within the same assembly (or other assemblies should you add friend’s)
    • Derive D from C

    EDIT

    This scenario is called out in section 3.5.3 of the C# spec.

    The reason this is not allowed is because it would allow for cross hierarchy calls. Imagine that in addition to D, there was another base class of C called E. If your code could compile it would allow D to access the member E.F. This type of scenario is not allowed in C# (and I believe the CLR but I don’t 100% know).

    EDIT2 Why this is bad

    Caveat, this is my opinion

    The reason this is now allowed is it makes it very difficult to reason about the behavior of a class. The goal of access modifiers is to give the developer control over exactly who can access specific methods. Imagine the following class

    sealed class MyClass : C {   override F(D d) { ... }  } 

    Consider what happens if F is a somewhat time critical function. With the current behavior I can reason about the correctness of my class. After all there are only two cases where MyClass.F will be called.

    1. Where it’s invoked in C
    2. Where I explicitly invoke it in MyClass

    I can examine these calls and come to a reasonable conclusion about how MyClass functions.

    Now, if C# does allow cross hierarchy protected access I can make no such guarantee. Anyone in a completely different assembly can come by and derive from C. Then they can call MyClass.F at will. This makes it completely impossible to reason about the correctness of my class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 104k
  • Answers 104k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Functional Test: Does it do the things marketing/usability/customers signed off… May 11, 2026 at 8:37 pm
  • Editorial Team
    Editorial Team added an answer Well, the answer is : if (is_uploaded_file(@$_FILES['ulimage']['tmp_name'])) { $targetfilename =… May 11, 2026 at 8:37 pm
  • Editorial Team
    Editorial Team added an answer So...with CookieOfFortune's help I tracked down the root of the… May 11, 2026 at 8:37 pm

Related Questions

While creating a file synchronization program in C# I tried to make a method
I wanted to make Map of Collections in Java, so I can make something
I know that calling a virtual method from a base class constructor can be
So, I've been working on some playing cards in Java. (Not for any practical

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.