检查 VB 中的通用对象是否相等
Check Generic objects for equality in VB
这看起来很微不足道,但我无法让它发挥作用。我需要比较两个相同类型 T
的泛型是否相等:
Sub SomeMethod(Of T)(x As T, y As T)
If x Is y
' do stuff
End If
End Sub
编译器拒绝:
'Is' operand of type 'T' can be compared only to 'Nothing' because 'T' is a type parameter with no class constraint.
像这样给它一个class约束:
Sub SomeMethod(Of T As Class)(x As T, y As T)
If x Is y Then
' do stuff
End If
End Sub
这看起来很微不足道,但我无法让它发挥作用。我需要比较两个相同类型 T
的泛型是否相等:
Sub SomeMethod(Of T)(x As T, y As T)
If x Is y
' do stuff
End If
End Sub
编译器拒绝:
'Is' operand of type 'T' can be compared only to 'Nothing' because 'T' is a type parameter with no class constraint.
像这样给它一个class约束:
Sub SomeMethod(Of T As Class)(x As T, y As T)
If x Is y Then
' do stuff
End If
End Sub