what different between new and override and when use new ?

ahmed salah 3,216 Reputation points
2022-08-03T23:29:38.897+00:00

I work on csharp 6 i don't neew different between new and overide for base class
and what real cases i used new on it
so can you help me

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Generics  
{  
    class A  
    {  
        public virtual void show()  
        {  
            Console.WriteLine("Hello: Base Class!");  
            Console.ReadLine();  
        }  
    }  
  
    class B : A  
    {  
        public override void show()  
        {  
            Console.WriteLine("Hello: Derived Class!");  
            Console.ReadLine();  
        }  
    }  
  
    class C : B  
    {  
        public new void show()  
        {  
            Console.WriteLine("Am Here!");  
            Console.ReadLine();  
        }  
    }  
Developer technologies | .NET | Other
Developer technologies | C#
{count} votes

3 answers

Sort by: Most helpful
  1. CowieCowie 76 Reputation points
    2022-08-04T02:45:55.16+00:00

    Hi ahmedsalah, there do exits some differences between keyword override and new. In daily coding, we dont recommend you use keyword **new**. The biggest difference between override and new is that once you "new" a method in derived class, you cant use polymorphism anymore.
    In other words, if you use the same method name in base class to "new" a method in derived class, the method in base class can still work as it defined in base method. However, if you use the same method name in base class to "override" a method in derived class, the method in base class can only work as it defined in override method. To make it more understandable, here`s some codes for you:

    • Override
       public class C1  
       {  
           public virtual string GetName()  
           {  
               return "AHMEDSALAH";  
           }  
       }  
      
       public class C2 : C1  
       {  
           public override string GetName()  
           {  
               return "ahmedsalah";  
           }  
       }  
      
        C1 c1 = new C1();  
        Console.WriteLine(c1.GetName());//Output “AHMEDSALAH”  
      
        C2 c2 = new C2();  
        Console.WriteLine(c2.GetName());//Output “ahmedsalah”  
      
        //Here`s the point!!!  
        C1 c3 = new C2();  
        Console.WriteLine(c3.GetName());//Output “ahmedsalah”   
      
    • New
           public class C1  
           {  
               public virtual string GetName()  
               {  
                   return "AHMEDSALAH";  
               }  
           }  
      
           public class C2 : C1  
           {  
               public new string GetName()  
               {  
                   return "ahmedsalah";  
               }  
           }  
      
            C1 c1 = new C1();  
            Console.WriteLine(c1.GetName());//Output “AHMEDSALAH”  
      
            C2 c2 = new C2();  
            Console.WriteLine(c2.GetName());//Output “ahmedsalah”  
      
            //Here`s the point!!!  
            C1 c3 = new C2();  
            Console.WriteLine(c3.GetName());//Output “AHMEDSALAH”   
      

    In the codes above, I believe you can understand the difference between keyword override and new.
    In summary, you can override many methods in different derived class and when you use derived class to new a base class, like C1 test = new C2(), you can only use the method in derived class. If you new method in derived class, you can use base method when you use derived class to new a base class.

    Besides, you need to pay attention to conditions in which you can use override (virtual method or blah blah...)
    So here`s the Microsoft doc link for details of override in C#, hope can help you more: override

    If you still feel confused, please comment and we can discuss more. If you feel helpful from my answer, please accept it and vote for it!

    0 comments No comments

  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-08-04T16:45:17.96+00:00

    class inheritance is about inserting methods. but what if your new class needs to change the behavior of an inherited method? and if you class is downcast, which method should be used?

    the combinations of sealed, virtual, new and override handle this.

    if a method is declared virtual, then it can be overridden in inherited class.
    if a method is declared sealed, then it can not be overridden. sealed is the default.

    to override a method, is to replace it in class hierarchy. so if a overrides a method in b, calling the in a actually call the b method. to use the override, the method must be visual in the parent.
    if you don't want to override the method, but just want replace only in the new class and its children, you use the new operator. the new operator works with both sealed and virtual parent methods.

    see this program (foo is a mix of sealed and virtual, bar is always virtual):

    using System;  
    class A  
    {  
    	public string Foo() => "A:Foo";  
    	public virtual string Bar() => "A:Bar";  
    }  
    class B:A  
    {  
    	public new string Foo() => "B:Foo";  
    	public override string Bar() => "B:Bar";  
    }  
    class C:B  
    {  
    	public new virtual string Foo() => "C:Foo";  
    	public override string Bar() => "C:Bar";  
    }  
    class D:C  
    {  
    	public override string Foo() => "D:Foo";  
    	public override string Bar() => "D:Bar";  
    }  
    public class Program  
    {  
    	public static void Main()  
    	{  
    		var d = new D();  
    		Console.WriteLine($"D: Foo() -> {d.Foo()} Bar() -> {d.Bar()}");  
    		var c = (C) d;  
    		Console.WriteLine($"C: Foo() -> {c.Foo()} Bar() -> {c.Bar()}");  
    		var b = (B) d;  
    		Console.WriteLine($"B: Foo() -> {b.Foo()} Bar() -> {b.Bar()}");  
    		var a = (A) d;  
    		Console.WriteLine($"A: Foo() -> {a.Foo()} Bar() -> {a.Bar()}");  
     	}`enter code here`  
    }  
    

    outputs:

    D: Foo() -> D:Foo Bar() -> D:Bar  
    C: Foo() -> D:Foo Bar() -> D:Bar  
    B: Foo() -> B:Foo Bar() -> D:Bar  
    A: Foo() -> A:Foo Bar() -> D:Bar  
    

    note: the complexity of what a method does in a long inheritance chain is why inheritance chains beyond two deep is considered an anti-pattern nowdays.

    0 comments No comments

  3. Rajanikant Hawaldar 91 Reputation points
    2022-08-21T11:54:17.39+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.