I have a class Score which is going to be heavily used in comparisons against integers. I was planning on overloading the == operator to enable these comparisons as per th
I might go ahead and define an implicit conversion from int
to Score
, so that when you deal with equality, you only need to deal with a single type.
public static implicit operator Score(int value)
{
return new Score { Value = value }; // or new Score(value);
}
// define bool operator ==(Score score1, Score score2)
// elsewhere
Score score = new Score { Value = 1 };
bool isScoreOne = (score == 1);
And while you're defining your own ==
operator, do remember to go ahead and define !=
, and override Equals
and GetHashCode
.