You need to implement Equals
method of Object
that does nothing else comparing references.
https://referencesource.microsoft.com/#mscorlib/system/object.cs,f2a579c50b414717,references
public class Field : IEquatable<Field>
{
public bool isEmptyField { get; set; }
public bool isPawn { get; set; }
public bool isDame { get; set; }
public string color { get; set; }
public Field(bool isEmptyField, bool isPawn, bool isDame, string color)
{
this.isEmptyField = isEmptyField;
this.isPawn = isPawn;
this.isDame = isDame;
this.color = color;
}
public override bool Equals(object obj)
{
return Equals(this, obj as Field);
}
public bool Equals(Field obj)
{
return Equals(this, obj);
}
static public bool Equals(Field x, Field y)
{
return ( ReferenceEquals(x, null) && ReferenceEquals(y, null) )
|| ( !ReferenceEquals(x, null)
&& !ReferenceEquals(y, null)
&& ( x.isEmptyField == y.isEmptyField )
&& ( x.isPawn == y.isPawn )
&& ( x.isDame == y.isDame )
&& ( x.color == y.color ) );
}
}
IEquatable<Field>
is not needed but can be usefull and it doesn't cost much.
Test
var field1 = new Field(true, true, true, "test");
var field2 = new Field(true, true, true, "test");
var field3 = new Field(false, true, true, "test");
var field4 = new Field(true, true, true, "test2");
Console.WriteLine(field1.Equals(field2));
Console.WriteLine(field1.Equals(field3));
Console.WriteLine(field1.Equals(field4));
Console.WriteLine(field1.Equals(null));
Console.WriteLine(Field.Equals(null, null));
Output
True
False
False
False
True