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

后端 未结 4 1380
伪装坚强ぢ
伪装坚强ぢ 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:14

    That would possibly work if it knew that where T : class, doing a reference comparison. Operators generally have very little support with generics, but there are workarounds. MiscUtil offers indirect support for operators on generics, otherwise EqualityComparer.Default.Equals(x,y) is a good choice.

提交回复
热议问题