Incorrect C# code for generic foo: "if (foo > null) ..."
GlennSlayden
Posts: 5 Bronze 1
The Reflector 10.0.6.546 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:
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:
[__DynamicallyInvokable] public bool Contains(T value) => this.Find(value) > null; |
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 } |