🔴 Redéfinition des fonctions de comparaisons (Equals & GetHashCode)

pull/45/head
Rémi LAVERGNE 1 year ago
parent c7a8f77e01
commit 1cd17ebff0

@ -2,15 +2,50 @@
public class Cell public class Cell
{ {
private Position pos; public Position Pos { get; private set; }
private int value;
private string type; private int? value;
private string background; public int? Value {
get
{
return value;
}
set
{
if (value < 0)
{
throw new Exception("La valeur doit être supérieure à 0");
}
this.value = value;
}
}
private string Type { get; set; }
private string? Background { get; set; }
public Cell(int x, int y) public Cell(int x, int y)
{ {
pos = new Position(x, y); Pos = new Position(x, y);
} }
public string GetCellType() => type; public string GetCellType() => Type;
// Redefinition de l'opérateur == pour comparer deux cellules
public override bool Equals(object? obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
Cell c = (Cell)obj;
return (Pos.X == c.Pos.X) && (Pos.Y == c.Pos.Y);
}
// Redefinition de la méthode de Hash pour l'opérateur ==
public override int GetHashCode()
{
return Pos.X * Pos.Y;
}
} }
Loading…
Cancel
Save