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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:16:51+00:00 2026-06-05T16:16:51+00:00

When inheriting an inherited class, the new / override behaviour is not what I

  • 0

When inheriting an inherited class, the new / override behaviour is not what I would expect:

$ cat Program.cs
using System;

class A {
    public virtual void SayHi() {
        Console.WriteLine("From A");
    }
}
class B : A { 
    public new virtual void SayHi()  {
        Console.WriteLine("From B");
    }
}
class C : B { 
    public override void SayHi() {
        Console.WriteLine("From C");
    }
}

public class Program {
    public static void Main() {
        A p = new C();
        p.SayHi();
    }
}

$ ./Program.exe 
From A

As class C overrides the sayHi() method I would expect the output to be From C. Why does the B class’s new modifier take precedence here? What is the use case for that? Especially as it breaks the obvious use case of having C really override A.

Note that the above code was run on Mono 2.10 running on a Debian-derived distro. But I have confirmed the same behaviour using the C# compiler in MS Visual Studio.

  • 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-05T16:16:53+00:00Added an answer on June 5, 2026 at 4:16 pm

    The new modifier causes member hiding, which breaks the polymorphic relationship in your class hierarchy. The SayHi method of B is treated as distinct (not an override) from A’s (thus the choice of the word “new” as keyword). C’s method then overrides B’s, not A’s (which remains hidden).

    Therefore, when you call SayHi on a C instance through an A reference, the runtime would resolve it against the A type, not the C type (within which SayHi is a “new” method inherited from B).

    If, on the other hand, you were to run:

    B p = new C();
    p.SayHi();
    

    …you would get the expected polymorphic result:

    From C
    

    Edit: Since you requested a use-case, here’s one. Before the introduction of generics in .NET Framework 2.0, member hiding was sometimes used as a means of altering the return types of inherited methods in derived classes (something you can’t do when overriding) in order to return more specific types. For example:

    class ObjectContainer
    {
        private object item;
    
        public object Item 
        {
            get { return item; }
            set { item = value; }
        }
    }
    
    class StringContainer : ObjectContainer
    {
        public new virtual string Item
        {
            get { return base.Item as string; }
            set { base.Item = value as string; }
        }
    }
    
    class QuotedStringContainer : StringContainer
    {
        public override string Item
        {
            get { return "\"" + base.Item + "\""; }
        }
    }
    

    The Item property of the ObjectContainer class returns a plain object. However, in StringContainer, this inherited property is hidden to return a string instead. Thus:

    ObjectContainer oc = new StringContainer();
    object o  = oc.Item;   // Valid, since ObjectContainer.Item is resolved
    string s1 = oc.Item;   // Not valid, since ObjectContainer.Item is still resolved
    string s2 = ((StringContainer)oc).Item;   
                           // Valid, since StringContainer.Item is now resolved
    

    The QuotedStringContainer class overrides the Item property of StringContainer, inheriting its string return type; however, it is still hidden from the object-returning Item property of ObjectContainer. If it were not this way, there would be no way of reconciling their disparate return types…

    ObjectContainer oc = new QuotedStringContainer();
    object o  = oc.Item;   // Valid, since ObjectContainer.Item is resolved
    string s1 = oc.Item;   // Not valid, since ObjectContainer.Item is still resolved
    string s2 = ((StringContainer)oc).Item;   
                           // Valid, since QuotedStringContainer.Item is now resolved
                           // (polymorphism!)
    string s3 = ((QuotedStringContainer)oc).Item;   
                           // Valid, since QuotedStringContainer.Item is now resolved
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple class inheriting from WebControl as follow: public class Instrument :
I've got an abstract class defined by another assembly (not editable): public abstract class
When I define a new class inheriting from NSObject : @interface Photo : NSObject
inheriting a class attribute from a super class and later changing the value for
I want to know if one class is inheriting from another, is it better
Let's say I create a class Planet that can be inherited. One of the
using C# I have a class which contains among other meta information the root
In the code following, OP created a class with inheriting Template class class BatchRename(Template):
According to the doc, object is all new-style classes' base class. And AFAIK, the
I have a generic object factory FactoryBase<T> with a factory method: public abstract class

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.