Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Foo
- {
- public int FooId { get; set; }
- public string FooName { get; set; }
- public override bool Equals(object obj)
- {
- Foo fooItem = obj as Foo;
- return fooItem.FooId == this.FooId;
- }
- public override int GetHashCode()
- {
- // Which is preferred?
- return base.GetHashCode();
- //return this.FooId.GetHashCode();
- }
- }
- unchecked //Solo es necesario si está compilando con los controles aritméticos
- {//Por defecto están deshabilitados, así la mayoría de la gente no lo necesitará.
- int hash = 13;
- hash = (hash * 7) + field1.GetHashCode();
- hash = (hash * 7) + field2.GetHashCode();
- ...
- return hash;
- }
- public class Gato
- {
- public string Nombre {get; set;}
- public string Raza {get; set;}
- public string NombrePoseedor {get; set;}
- }
- //dos instancias mismas propiedades
- var GatoUno= new Gato{Nombre="asrael",Raza="comun", NombrePoseedor="Gargamel"};
- var GatoDos= new Gato{Nombre="asrael",Raza="comun" ,NombrePoseedor="Gargamel"};
- //declarando una comparacion
- Console.WriteLine($"Gatos Iguales? = { GatoUno.Equals(GatoDos) }");
- //Gatos Iguales? = false
- var gatosDictionary = new Dictionary<Gatos, int> { { GatoUno, 1 } };
- var salida= gatosDictionary.ContainsKey(GatoDos) ;
- Console.WriteLine($"Ya existe? {salida}");
- //false
- Console.WriteLine(GatoUno.GetHashCode()); // 3423423
- Console.WriteLine(GatoDos.GetHashCode()); // 465464
- public override int GetHashCode() =>
- new { Nombre, NombrePoseedor }.GetHashCode();
- var salida= gatosDictionary.ContainsKey(GatoDos) ;
- //salida= true
- Espero haya quedado claro la rapida explicacion,
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement