why result of execution is ClassB and not ClassC ?

ahmed salah 3,131 Reputation points
2021-10-16T11:26:40.433+00:00

I Working on console application I ask why result is
ClassB
IT must be ClassC
i executed app on console and it give me ClassB
why final result ClassB i expect it will be ClassC

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace apptest
{
    class Program
    {
        static void Main(string[] args)
        {
            classA a = new classC();
            Console.WriteLine(a.Print());
            Console.ReadKey();
        }
        public class classA
        {
            public virtual string Print()
            {
                return "ClassA";
            }

        }
        public class classB: classA
        {
            public override string Print()
            {
                return "ClassB";
            }

        }
        public class classC: classB
        {
            public new string Print()
            {
                return "ClassC";
            }
        }
    }
}
.NET Standard
.NET Standard
A formal specification of .NET APIs that are available on multiple .NET implementations.
493 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,587 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: The period of time during which a program is being executed in a computer.
981 questions
No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 89,061 Reputation points
    2021-10-16T13:41:11.747+00:00

    If you expect "ClassC", then replace new with override:

    public class classC : classB
    {
       public override string Print( )
       {
          return "ClassC";
       }
    }
    			
    
  2. Bruce (SqlWork.com) 31,496 Reputation points
    2021-10-16T15:54:52.19+00:00

    Classes really implement an interface. A virtual override replaces the virtual method, so if you downcast it’s the same method. The new operator replaces the method only for an instance is cast as that class.

    So

    classA a = new classC();
    Console.WriteLine(((classC)a).Print());
    

    Would print ClassC

  3. Xingyu Zhao-MSFT 5,341 Reputation points
    2021-10-18T06:26:00.227+00:00

    Hi @ahmed salah ,
    Take a look at the following document:
    Knowing When to Use Override and New Keywords (C# Programming Guide)

    • By using new, you are asserting that you are aware that the member that it modifies hides a member that is inherited from the base class.

    Here's an example in the document which can help you understand the difference between 'override' and 'new'.

        class Program    
        {    
            static void Main(string[] args)    
            {    
                BaseClass bc = new BaseClass();    
                DerivedClass dc = new DerivedClass();    
                BaseClass bcdc = new DerivedClass();    
    
                // The following two calls do what you would expect. They call    
                // the methods that are defined in BaseClass.    
                bc.Method1();    
                bc.Method2();    
                // Output:    
                // Base - Method1    
                // Base - Method2    
    
                // The following two calls do what you would expect. They call    
                // the methods that are defined in DerivedClass.    
                dc.Method1();    
                dc.Method2();    
                // Output:    
                // Derived - Method1    
                // Derived - Method2    
    
                // The following two calls produce different results, depending  
                // on whether override (Method1) or new (Method2) is used.    
                bcdc.Method1();    
                bcdc.Method2();    
                // Output:    
                // Derived - Method1    
                // Base - Method2    
            }    
        }    
    
        class BaseClass    
        {    
            public virtual void Method1()    
            {    
                Console.WriteLine("Base - Method1");    
            }    
    
            public virtual void Method2()    
            {    
                Console.WriteLine("Base - Method2");    
            }    
        }    
    
        class DerivedClass : BaseClass    
        {    
            public override void Method1()    
            {    
                Console.WriteLine("Derived - Method1");    
            }    
    
            public new void Method2()    
            {    
                Console.WriteLine("Derived - Method2");    
            }    
        }    
    

    So if you want your code give you 'ClassC', just:

                classC c = new classC();  
                Console.WriteLine(c.Print());  
    

    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.