New vs Override

Asked

Viewed 332 times

8

What would be the difference between new and override ?
Examples:
Override:

public class ClasseBase
{
    public virtual void Funcao ()
    {

    }
}
public class Classe : ClasseBase
{
     public override void Funcao()
     {

     }
}

New:

public class ClasseBase
{
    public void Funcao()
    {

    }
}
public class Classe : ClasseBase
{
     public new void Funcao()
     {

     }
}

There is a difference between the new and the override? I know that the new can be used to instantiate a class and the override no, but if there is a difference, what is?

1 answer

12


The difference is that if the method is declared with new is not polymorphic whereas with override is.

Soon override gives a new implementation to the base class method, and new it is as if it were a method different from the base class although it has the same name.

Taking your example:

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

public class Classe : ClasseBase
{
    public override void Funcao() //com override
    {
        Console.WriteLine("Derivada");
    }
}

public class Classe2 : ClasseBase
{
    public new void Funcao() //com new
    {
        Console.WriteLine("Derivada2");
    }
}

When we create objects and store them in a variable like this class, everything works normally:

 Classe c1 = new Classe();
 Classe2 c2 = new Classe2();

 c1.Funcao(); //Derivada
 c2.Funcao(); //Derivada2

But when we store it in the type of the base class and wait for the polymorphism to act, running the code of the derived classes we see that this only happens with override:

ClasseBase c3 = new Classe();
ClasseBase c4 = new Classe2();

c3.Funcao(); //Derivada
c4.Funcao(); //Base

Here only in c3 the call was polymorphic by running the method Funcao of Classe instead of ClasseBase which was the type of the variable.

See the example in . netFiddle

The new allows a new implementation of a method nonvirtual of the base class, thus hiding the implementation of the base.

Example:

public class ClasseBase
{
    public void Funcao() //não virtual
    {
    }
}

public class Derivada 
{
    public new void Funcao()
    {
    }
}

Something that Visual Studio itself indicates when it sees a function equal to the base class being non-virtual:

inserir a descrição da imagem aqui

If you look closely at the image you will see that it is a warning and not a mistake, which makes it not strictly mandatory in this case, although it gives more legibility and clarity to the code.

Documentation for the new and override

References:

  • here is a good reference: https://docs.microsoft.com/pt-br/dotnet/csharp/programming-guide/classes-and-structs/knowing-when-to-use-override-new-keywords if you want to of course add!

  • 1

    @Virgilionovic Yes of course. I appreciate the comment and the reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.