The following code:
if (value != (T)defaultView.CurrentItem)
Is resolved at compile-time, not runtime, to use object
's !=
operator, which is simple reference comparison. It doesn't matter that T
might overload/replace the meaning of !=
, such as string
's overloading to compare it via value instead of reference. Thus, it is not correct to compare generic types in such a way if you're expecting value comparison, and e.g. two strings with the value "New"
might have new1 != new2
be true
.
You could use your EqualityComparer<T>.Default
code, which can make use of T
's override
of object.Equals(object)
, or you can use the static object.Equals(object, object)
, which also uses the type's overridden equality comparison. The only major difference here is that EqualityComparer<T>.Default
will prefer to use the implementation provided by IEquatable<T>
, if it exists.
if (!object.Equals(value, defaultView.CurrentItem))