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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:49:38+00:00 2026-05-15T04:49:38+00:00

I’m having the following classes: class Base { public virtual void Print() { Console.WriteLine(Base);

  • 0

I’m having the following classes:

class Base
{
    public virtual void Print()
    {
        Console.WriteLine("Base");
    }
}

class Der1 : Base
{
    public new virtual void Print()
    {
        Console.WriteLine("Der1");
    }
}

class Der2 : Der1
{
    public override void Print()
    {
        Console.WriteLine("Der2");
    }
}

This is my main method:

Base b = new Der2();
Der1 d1 = new Der2();
Der2 d2 = new Der2();

b.Print();
d1.Print();
d2.Print();

The output is Base, Der2, Der2.

As far as I know, Override won’t let previous method to run, even if the pointer is pointing to them. So the first line should output Der2 as well. However Base came out.

How is it possible? How the override didn’t work 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. Editorial Team
    Editorial Team
    2026-05-15T04:49:38+00:00Added an answer on May 15, 2026 at 4:49 am

    You’ve never actually overriden the Base version of Print(). You’ve only hidden it with a separate virtual method (named the same) in Der1.

    When you use the new keyword on a method signature – you are telling the compiler that this is a method that happens to have the same name as a method of one of your base classes – but has no other relation. You can make this new method virtual (as you’ve done) but that’s not the same as overriding the base class method.

    In Der2 when you override Print you are actually overriding the ‘new’ version that you declared in Der1 – not the version is Base. Eric Lippert has an excellent answer to a slightly different question that may help you reason about how virtual methods are treated in the C# language.

    In your example, when you call Print, you are calling it in the first case through a reference of type Base – so the hidden (but not overriden) version of Print is called. The other two calls are dispatched to Der1‘s implementation, because in this case, you’ve actually overriden the method.

    You can read more about this in the MSDN documentation of new and override.

    What you may have intended to do with Der1 (as you did with Der2) is to use the override keyword:

    class Base 
    { 
        public virtual void Print() 
        { 
            Console.WriteLine("Base"); 
        } 
    } 
    
    class Der1 : Base 
    { 
        // omitting 'new' and using override here will override Base
        public override void Print() 
        { 
            Console.WriteLine("Der1"); 
        } 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 414k
  • Answers 414k
  • 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 Swf file with same properties should be cached in first… May 15, 2026 at 8:44 am
  • Editorial Team
    Editorial Team added an answer You can use bind() for this. For example, if you… May 15, 2026 at 8:44 am
  • Editorial Team
    Editorial Team added an answer It sounds to me like the thing to do here… May 15, 2026 at 8:44 am

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.