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

  • Home
  • SEARCH
  • 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 162641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:25:38+00:00 2026-05-11T11:25:38+00:00

The following program prints A:C(A,B) B:C(A,B) (as it should) public interface I { string

  • 0

The following program prints

A:C(A,B) B:C(A,B) 

(as it should)

public interface I {     string A(); }  public class C : I {     public string A()     {         return 'A';     }      public string B()     {         return 'B';     } }  public class A {     public virtual void Print(C c)     {         Console.WriteLine('A:C(' + c.A() + ',' + c.B() + ')');     } }  public class B : A {     public new void Print(C c)     {         Console.WriteLine('B:C(' + c.A() + ',' + c.B() + ')');     }      public void Print(I i)     {         Console.WriteLine('B:I(' + i.A() + ')');     } }  class Program {     public static void Main(string[] args)     {         A a = new A();         B b = new B();         C c = new C();         a.Print(c);         b.Print(c);     } } 

however, if I change keyword ‘new’ to ‘override’ in class B like so:

    public override void Print(C c) 

all of a sudden program starts to print:

A:C(A,B) B:I(A) 

Why?

  • 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-11T11:25:38+00:00Added an answer on May 11, 2026 at 11:25 am

    This is to do with how overloaded methods are resolved.

    Effectively (simplified somewhat), the compiler first looks at the declared type of the expression (B) in this case and looks for candidate methods which are first declared in that type. If there are any methods which are appropriate (i.e. where all the arguments can be converted to the method’s parameter types) then it doesn’t look at any parent types. This means that overridden methods, where the initial declaration is in a parent type, don’t get a look-in if there are any ‘freshly declared’ appropriate methods in the derived type.

    Here’s a slightly simpler example:

    using System;  class Base {     public virtual void Foo(int x)     {         Console.WriteLine('Base.Foo(int)');     } }  class Derived : Base {     public override void Foo(int x)     {         Console.WriteLine('Derived.Foo(int)');     }      public void Foo(double d)     {         Console.WriteLine('Derived.Foo(double)');     } }  class Test {     static void Main()     {         Derived d = new Derived();         d.Foo(10);     } } 

    This prints Derived.Foo(double) – even though the compiler knows there is a matching method with a parameter of type int, and the argument is type int, and the conversion from int to int is ‘better’ than the conversion from int to double, the fact that only the Foo(double) method is originally declared in Derived means the compiler ignores Foo(int).

    This is highly surprising IMO. I can see why it would be the case if Derived didn’t override Foo – otherwise introducing a new, more specific, method in the base class could change the behaviour unexpectedly – but clearly Derived here knows about Base.Foo(int) as it’s overriding it. This is one of the (relatively few) points where I believe the C# designers made the wrong decision.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you want to catch it in your form, you… May 12, 2026 at 1:46 pm
  • Editorial Team
    Editorial Team added an answer Your problem translates to this: you have an XSM file… May 12, 2026 at 1:46 pm
  • Editorial Team
    Editorial Team added an answer The reason that it fails is because TComponent could actually… May 12, 2026 at 1:46 pm

Related Questions

please correct the program. Q. Write a java prg to accept RollNo, Name of
I need a language lawyer with authoritative sources. Take a look at the following
I have a program which gets commands as a string. Each character in the
I am having problems with the greps in Emacs. a) grep doesnt seem to

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.