fork download
  1. // ===++===
  2. //
  3. // OrtizOL
  4. //
  5. // ===--===
  6. /*============================================================
  7. //
  8. // Clase: Producto.cs
  9. //
  10. // PropĆ³sito: Uso de la interfaz IEquatable.
  11. //
  12. ============================================================*/
  13.  
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17.  
  18. namespace Recetas.Cap02
  19. {
  20. internal class Producto : IEquatable<Producto>
  21. {
  22. public string Nombre
  23. {
  24. get;
  25. set;
  26. }
  27.  
  28. public int Codigo
  29. {
  30. get;
  31. set;
  32. }
  33.  
  34. public bool Equals(Producto p)
  35. {
  36. if (Object.ReferenceEquals(p, null))
  37. {
  38. return false;
  39. }
  40.  
  41. if (Object.ReferenceEquals(this, p))
  42. {
  43. return true;
  44. }
  45.  
  46. return Codigo.Equals(p.Codigo) && Nombre.Equals(p.Nombre);
  47. }
  48.  
  49. public override int GetHashCode()
  50. {
  51. int hashNombre = Nombre == null ? 0 : Nombre.GetHashCode();
  52.  
  53. int hashCodigo = Codigo.GetHashCode();
  54.  
  55. return hashNombre ^ hashCodigo;
  56. }
  57. }
  58.  
  59. internal class PruebaProducto
  60. {
  61. static public void Main()
  62. {
  63. Producto[] productos = {
  64. new Producto { Nombre = "Teclado", Codigo = 31001},
  65. new Producto { Nombre = "Kindle", Codigo = 97001},
  66. new Producto { Nombre = "Mouse", Codigo = 72004},
  67. new Producto { Nombre = "Kindle", Codigo = 97001}
  68. };
  69.  
  70. // RemociĆ³n de duplicados:
  71. IEnumerable<Producto> sinDuplicados = productos.Distinct();
  72.  
  73. foreach (Producto producto in sinDuplicados)
  74. {
  75. Console.WriteLine("{0} - {1}", producto.Nombre, producto.Codigo);
  76. }
  77. }
  78. }
  79. }
Success #stdin #stdout 0.05s 34104KB
stdin
Standard input is empty
stdout
Teclado - 31001
Kindle - 97001
Mouse - 72004