I am trying to write a generic equality comparer. I've written the following code.
public static bool Equals<T>(T t1, T t2)
{
bool result;
if (typeof(T).IsPrimitive)
{
result = t1 == t2;
}
else
{
if (t1 == null)
{
result = t2 == null;
}
else
{
result = t1.Equals(t2);
}
}
return result;
}
However due to the compile error Operator '==' cannot be applied to operands of type 'T' and 'T'
for the line result = t1 == t2;
, I cannot use this code. Is there any other way to achieve this? I want to be able to use this method on any T (class, struct, primitive)
This question is not a duplicate of this question. As I wrote in the comment, I do not have to use ==
. It is just one of the ways I have tried