Operator '==' can't be applied to type T?

后端 未结 4 1349
伪装坚强ぢ
伪装坚强ぢ 2021-01-11 12:21

I thought this method was valid but I was wrong:

static void Equals(T x, T y)
{
    return x == y;    //operator == can\'t be applied to type T
}
         


        
4条回答
  •  孤街浪徒
    2021-01-11 13:24

    I like using EqualityComparer.Default for this.

    It is based on the overridden Equals method, but uses IEquatable when available, avoiding boxing on value types implementing it.

    EqualityComparer.Default.Equals(x, y)
    

提交回复
热议问题