What is the best (most concise and optimal) way to compare two instances of same generic type such that reference types are compared for identity (same object, so not calling Equals
) and value types for value equality.
Currently I do this:
static bool IdentityEquals<T>(T x, T y)
{
return typeof(T).IsValueType
? EqualityComparer<T>.Default.Equals(x, y)
: ReferenceEquals(x, y);
}
You should be able to just use object.Equals
for value types:
return typeof(T).IsValueType
? object.Equals(x, y)
: ReferenceEquals(x, y);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With