Can't operator == be applied to generic types in C#?

前端 未结 12 670
囚心锁ツ
囚心锁ツ 2020-11-22 02:21

According to the documentation of the == operator in MSDN,

For predefined value types, the equality operator (==) returns true if th

12条回答
  •  故里飘歌
    2020-11-22 02:46

    
    bool Compare(T x, T y) where T : class { return x == y; }
    
    

    The above will work because == is taken care of in case of user-defined reference types.
    In case of value types, == can be overridden. In which case, "!=" should also be defined.

    I think that could be the reason, it disallows generic comparison using "==".

提交回复
热议问题