Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

137
Visualizações
C# abstract record with abstract Equals method

Why are we not allowed to do the following for a record

abstract record AA
{
  public abstract bool Equals(AA other);
}

record BB:AA
{
  public override bool Equals(AA other)// error as it is already implemented
  {
    //do some thing
  }
}

while it is totally acceptable for classes?

abstract class AA
{
   public abstract bool Equals(AA other);
}

class BB:AA
{
  public override bool Equals(AA other)
  {
    //do some thing
  }
}

By the way, I am doing this implementation to enforce the Equals check to cascade to its derived classes.

Edit: just to give context on why am I interested on this is because I am currently creating an library/autogenerator for IEquatable.

Edit/Info 2: Based on the comments, I have done some tests. since the abstract Equals method of record can't be overridden, I tried leaving it as is.

public abstract record AA
{
    public int Prop1 { get; set; }
    public string? Prop2 { get; set; }
    public string? Prop5 { get; set; }
    public abstract bool Equals(AA? other);
}

public record BB : AA
{
    public string? Prop3 { get; set; }
}

The result I get an error of System.BadImageFormatException: Bad IL format.

All in all, abstract Equals method on records is not just an unnecessary implementation but is also a bad one.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This is because compiler already implements equality methods for records. Check Value equality of records:

To implement value equality, the compiler synthesizes the following methods:

  • An override of Object.Equals(Object).

  • This method is used as the basis for the Object.Equals(Object, Object) static method when both parameters are non-null.

  • A virtual Equals method whose parameter is the record type. This method implements IEquatable.

  • An override of Object.GetHashCode().

  • Overrides of operators == and !=.

It means that there's not need to enforce the implementation of Equals method in the base abstract record.

If you still want to have custom Equals method implementation and derive from some base record you could do so by declaring virtual Equals method with the derived type as an argument:

abstract record AA
{
    // Redundant in case of records and can be omitted.
    public abstract bool Equals(AA other);
}

record BB : AA
{
    public virtual bool Equals(BB other)
    {
        throw new NotImplementedException();
    }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda