Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

Comparando objetos

Certo, entendi que mesmo os atributos da classe tendo valores iguais não posso fazer uma comparação genérica utilizando sua variável de referência para comparar os objetos, caso eu quisesse fazer uma comparação eu teria que fazer item por item por exemplo: mauricio.numero == mauricio2.numero, mas, e se eu quiser realmente comparar os objetos, quero saber se a classe daquela variável tal é igual a que outra tal, como faço essa comparação?

2 respostas
solução!

Fala ai Theo, de boa ?

Cara tem uma forma bem legal que é implementar essa interface aqui

Ela vai te obrigar a implementar dois métodos Object.Equals e Object.GetHashCode, ai você vai fazer a sua implementação para comparar.

Tipo assim :


public class Person : IEquatable<Person>
{
    public int Age { get; set; }
    public string FirstName { get; set; }
    public Address Address { get; set; }

    public override bool Equals(object obj)
    {
        return this.Equals(obj as Person);
    }

    public bool Equals(Person other)
    {
        if (other == null)
            return false;

        return this.Age.Equals(other.Age) &&
            (
                object.ReferenceEquals(this.FirstName, other.FirstName) ||
                this.FirstName != null &&
                this.FirstName.Equals(other.FirstName)
            ) &&
            (
                object.ReferenceEquals(this.Address, other.Address) ||
                this.Address != null &&
                this.Address.Equals(other.Address)
            );
    }
}

public class Address : IEquatable<Address>
{
    public int HouseNo { get; set; }
    public string Street { get; set; }
    public City City { get; set; }

    public override bool Equals(object obj)
    {
        return this.Equals(obj as Address);
    }

    public bool Equals(Address other)
    {
        if (other == null)
            return false;

        return this.HouseNo.Equals(other.HouseNo) &&
            (
                object.ReferenceEquals(this.Street, other.Street) ||
                this.Street != null &&
                this.Street.Equals(other.Street)
            ) &&
            (
                object.ReferenceEquals(this.City, other.City) ||
                this.City != null &&
                this.City.Equals(other.City)
            );
    }
}

public class City : IEquatable<City>
{
    public string Name { get; set; }

    public override bool Equals(object obj)
    {
        return this.Equals(obj as City);
    }

    public bool Equals(City other)
    {
        if (other == null)
            return false;

        return
            object.ReferenceEquals(this.Name, other.Name) ||
            this.Name != null &&
            this.Name.Equals(other.Name);
    }
}

Obrigado Matheus Brandino, era isso mesmo que queria saber!

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software