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 112243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:30:50+00:00 2026-05-11T02:30:50+00:00

In the code below I tried in two ways to access the parent version

  • 0

In the code below I tried in two ways to access the parent version of methodTwo, but the result was always 2. Is there any way to get the 1 result from a ChildClass instance without modifying these two classes?

class ParentClass {     public int methodOne()     {         return methodTwo();     }      virtual public int methodTwo()     {         return 1;     } }  class ChildClass : ParentClass {     override public int methodTwo()     {         return 2;     } }  class Program {     static void Main(string[] args)     {         var a = new ChildClass();         Console.WriteLine('a.methodOne(): ' + a.methodOne());         Console.WriteLine('a.methodTwo(): ' + a.methodTwo());         Console.WriteLine('((ParentClass)a).methodTwo(): '          + ((ParentClass)a).methodTwo());         Console.ReadLine();     } } 

Update ChrisW posted this:

From outside the class, I don’t know any easy way; but, perhaps, I don’t know what happens if you try reflection: use the Type.GetMethod method to find the MethodInfo associated with the method in the ParentClass, and then call MethodInfo.Invoke

That answer was deleted. I’m wondering if that hack could work, just for curiosity.

  • 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-11T02:30:51+00:00Added an answer on May 11, 2026 at 2:30 am

    At the IL level, you could probably issue a call rather than a callvirt, and get the job done – but if we limit ourselves to C# ;-p (edit darn! the runtime stops you: VerificationException: ‘Operation could destabilize the runtime.’; remove the virtual and it works fine; too clever by half…)

    Inside the ChildClass type, you can use base.methodTwo() – however, this is not possible externally. Nor can you go down more than one level – there is no base.base.Foo() support.

    However, if you disable polymorphism using method-hiding, you can get the answer you want, but for bad reasons:

    class ChildClass : ParentClass {     new public int methodTwo() // bad, do not do     {         return 2;     } } 

    Now you can get a different answer from the same object depending on whether the variable is defined as a ChildClass or a ParentClass.

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

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • 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 How do you determine what PostgreSQL is storing? How do… May 11, 2026 at 11:39 pm
  • Editorial Team
    Editorial Team added an answer In 3.0, this is quite hard; but was improved in… May 11, 2026 at 11:39 pm
  • Editorial Team
    Editorial Team added an answer RewriteRule ^(.*)/details index.php?q=$1&details=true [L,QSA] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d… May 11, 2026 at 11:39 pm

Related Questions

I'm trying to figure out how to format the code below so that the
The example program below compiles two in-memory assemblies. The first compilation works fine. The
I have image, and on mouse over it fades in two arrows (on the
On my laptop with Intel Pentium dual-core processor T2370 (Acer Extensa) I ran a

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.