I'm trying the following:
T value1 = el.value; // it's of type T already
T value2 = default(T);
if (value1 != value2) // gives the following error: Operator '!=' cannot be applied to operands of type 'T' and 'T'
{
// ...
}
So, how could I compare both values? And why do this error occur?
Thanks in advance!
You can either use a constraint of where T : IEquatable<T>
as Henk mentioned, or ignore constraints and use:
if (!EqualityComparer<T>.Default.Equals(value1, value2))
Your surrounding generic class should list a constraint: T : IEquatable<T>
And then you have to use value1.Equals(value2)
The reason for all this is that not all types define operator ==
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