question

ahmedsalah-1628 avatar image
0 Votes"
ahmedsalah-1628 asked XingyuZhao-MSFT edited

why result of execution is ClassB and not ClassC ?

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";
             }
         }
     }
 }


dotnet-csharpdotnet-runtimedotnet-standard
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered

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

 public class classC : classB
 {
    public override string Print( )
    {
       return "ClassC";
    }
 }

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered XingyuZhao-MSFT edited

Hi @ahmedsalah-1628 ,
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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.