I have a Student
class that has an object of class Name
.
At this point, the equality check of the Student
class returns false, and I don't know why.
public class Student : IEquatable<Student>
{
public Name Name { get; }
public Student(Name name) => Name = name;
public bool Equals(Student other)
{
if (ReferenceEquals(this, other))
return true;
if (ReferenceEquals(other, null))
return false;
return Name == (other.Name);
}
}
public class Name : IEquatable<Name>
{
public string First { get; }
public Name(string first) => First = first;
public bool Equals(Name other)
{
if (ReferenceEquals(this, other))
return true;
if (ReferenceEquals(other, null))
return false;
return First == other.First;
}
}
var s1 = new Student(new Name("A"));
var s2 = new Student(new Name("A"));
Console.WriteLine(s1.Equals(s2).ToString());
Of course, doing the equality check this way will return true.
var s1 = new Student(new Name("A"));
var s2 = s1;
Console.WriteLine(s1.Equals(s2).ToString());
Can you tell me what I'm doing wrong?