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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:58:38+00:00 2026-06-14T05:58:38+00:00

Expecting Hello from the derived. but getting Hello from the base.. class Program {

  • 0

Expecting “Hello from the derived.” but getting “Hello from the base.”.

class Program
{
    interface IBase
    {
        void Method();
    }

    public class Base: IBase
    {
        public virtual void Method()
        {
            Console.WriteLine("Hello from the base.");
        }
    }

    public class Derived : Base
    {
        public virtual new void Method()
        {
            Console.WriteLine("Hello from the derived.");
        }
    }

    static void Main(string[] args)
    {
        IBase x = new Derived();
        x.Method();
    }
}

So why isn’t the derived class’s method called. And more importantly, how can I get the derived classes method to get called without casting x to the Derived type?

In my actual application, IBase has several other related methods and Derived only replaces two of the methods in IBase.

  • 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. Editorial Team
    Editorial Team
    2026-06-14T05:58:38+00:00Added an answer on June 14, 2026 at 5:58 am

    When you use the new modifier you are specifically saying that the method is not part of the virtual dispatch chain for that hierarchy, so calling the method by the same name in the base class will not result in redirection to the child class. If you mark the method with override instead of new then you will see the virtual dispatch that you are expecting to see.

    You will also need to remove virtual from the derived class’s method as you cannot mark an override method as virtual (it already is).

    If you really don’t want to override the method then it may be more appropriate, in your situation, to not use inheritance at all. You may simply want to use interfaces exclusively:

    public interface IFoo
    {
        void Foo();
    }
    
    public class A : IFoo
    {
        public void Foo()
        {
            Console.WriteLine("I am A, hear me roar!");
        }
    }
    
    public class B : IFoo
    {
        public void Foo()
        {
            Console.WriteLine("I am B, hear me roar!");
        }
    }
    
    private static void Main(string[] args)
    {
        IFoo foo = new A();
        foo.Foo();
    
        foo = new B();
        foo.Foo();
    
        Console.WriteLine();
        Console.WriteLine("Press any key to exit . . .");
        Console.ReadKey(true);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

By design I'm expecting a DuplicateKeyExeception from time to time. Is it possible to
I was expecting the below scenario common, but couldn't find much help online. I
My IDE is expecting a semicolon but I'm not sure where! It is this
Hello, i am trying to make Eclipse-CDT (Indigo) to indent C++ class methods relativly
i'm trying a basic hello work example getting json, auto mapping it and then
I'm having trouble figuring out what my Flex Project is expecting from my PHP
I am wondering how come I can reach inner class directly from outer type
(edited to include suggestions from Martin Honnen) Hello All, I've been trying to get
i want to call an exe file(abc.exe) from another application(hello) how can i get
I am trying to log from my C# Custom Action using session.Log( Hello World!

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.