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

前端 未结 12 816
囚心锁ツ
囚心锁ツ 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:48

    In general, EqualityComparer.Default.Equals should do the job with anything that implements IEquatable, or that has a sensible Equals implementation.

    If, however, == and Equals are implemented differently for some reason, then my work on generic operators should be useful; it supports the operator versions of (among others):

    • Equal(T value1, T value2)
    • NotEqual(T value1, T value2)
    • GreaterThan(T value1, T value2)
    • LessThan(T value1, T value2)
    • GreaterThanOrEqual(T value1, T value2)
    • LessThanOrEqual(T value1, T value2)

提交回复
热议问题