4 ms·
> An interesting case is the is not null pattern. This will check whether the reference is not null. Using != null may check something different when the type o
by belinder 6y ago
> An interesting case is the is not null pattern. This will check whether the reference is not null. Using != null may check something different when the type overloads the != operator.
If it doesn't use the != operator then what does it use? How does it work with Nullable<T> which relies on that
- rzzzt 6y agoProbably always the default implementation of != (reference non-equality for objects), irrespective of whether an overloaded version exists. This SO answer has a snippet of the specification that may be relevant: https://stackoverflow.com/a/7346086 https://stackoverflow.com/a/7346086
- MarkSweep 6y agoI think this sharplab link might help[1]. You can see that pattern matching generates the same code as using "== null". You can see that Nullable<T> is handled when the parameter type is a nullable int. When a Nullable<T> that has no value (HasValue == false) is cast to Object, it gets converted to a null reference. So the comparison will work as expected. When operating overloading is involved[2], different code is generated. [1]: https://sharplab.io/#v2:C4LglgNgPgAgTARgLACgYGYAEMEDZtyYDCmA3qppdljvgEYD2DEmAxgBYCmrA1gPJ0AVt2AAFAIbBgnAE4A7ABQDhrYJgYBKMhSq6YAdnWYwAZ0xyArhAgBuHZQC+96tjyZGzNl17KRSoSLqWuQounqGDJgAvFHmVrbOTijOGK70TCwc3DwAcvHidBCcAJJyYpLS8gpgZQD8QdqhYZQGRqZx1nZNjik0bh6Z3rn5hSVl1XUNIc0tEdGxlp2JqA5AA=== https://sharplab.io/#v2:C4LglgNgPgAgTARgLACgYGYAEMEDZtyYDCmA... [2]: https://sharplab.io/#v2:C4LglgNgPgAgTARgLACgYGYAE9MGFMDeqmJ2WMCAbJgEYD2dEmAxgBYCmzA1rgK4DOwOgFsA8jQBWnYAAUAhsGDsATgDsAFPjoBKQsVIGYAdkx1MYfplW8IEANz6SAX0dlsVWgyZtOPAULFJaU1TXSIUA0MTMwBeGKsbe1cXCNJXDHdqekZTAAcVBTplTDiQgA8AGjxMAE8w1yjMADM5CH52B1TnV3TyD2ymOnzlQuKAQhjyqvw6vS7I42bW9s6DFKcgA=== https://sharplab.io/#v2:C4LglgNgPgAgTARgLACgYGYAE9MGFMDeqmJ2...