Can't compare T value1 with T value2 = default(T). Why and how to do that on C#?

后端 未结 7 818
情话喂你
情话喂你 2020-12-19 05:53

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: Operato         


        
相关标签:
7条回答
  • 2020-12-19 06:37

    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 ==

    0 讨论(0)
提交回复
热议问题