What are the challenges you face when working across database platforms? Take the survey

Incorrect C# code for generic foo: "if (foo > null) ..."

The Reflector C# decompilation of System.Collections.Generic.LinkedList<T>.Contains(...) in C:\WINDOWS\Microsoft.net\Framework\v4.0.30319\System.dll (.NET 4.7.2) is not correct:

public bool Contains(T value) => this.Find(value) > null;
This is never valid C# code regardless of T being a reference or value-type (it's unconstrained here), because 'null' can never be compared with an inequality ("greater than").

For completeness, here is the corresponding IL:

.method public hidebysig newslot virtual final instance bool Contains(!T 'value') cil managed
    .custom instance void __DynamicallyInvokableAttribute::.ctor()
    .maxstack 8
    L_0000: ldarg.0 
    L_0001: ldarg.1 
    L_0002: call instance class System.Collections.Generic.LinkedListNode`1<!0> ">System.Collections.Generic.LinkedList`1<!T>::Find(!0)
    L_0007: ldnull 
    L_0008: cgt.un 
    L_000a: ret 
Sign In or Register to comment.