Learnt about a #CSharp feature I didn’t know existed reading this reddit thread today…
Tho I’m not sure why someone would want to override the != operator?
https://www.reddit.com/r/csharp/s/ig1EiG9rRM
🤔
Tho I’m not sure why someone would want to override the != operator?
https://www.reddit.com/r/csharp/s/ig1EiG9rRM
🤔
Comments
For instance if (obj is { Something: true }) does both a null check, as well as checking if the prop is true, which is nicer than if (obj != null && obj.Something == true)
Having a look at the compiled code, if there's an operator overload then it'll cast `input` to object: https://sharplab.io/#v2:C4LglgNgNAJiDUAfAAgJgIwFgBQyDMABGgQMIEDeOB1RhyALAQLIAUAlBVTd199SQH4CAZwIBeAgDsArhAgBuXnyXcwAMwItRAQgky5HStj4mi6AJwsARADkA9sCmyIVtouOmAvipo/q6zVEwUUkHJwNOD1MaZAtre0d9Fzc/Am8o5Qz0ngz8MwA2AgAjOzsIAjsABwBTACcAQ2A7WoJdFkECeqgKooAraoBjYCEitlSxAD5OgDoAUQBHaXqIYRYkjgAyDeK5xeXV9fEJNX3q9xzuPNjCkrKKmoamlrExdqEunv6hkY5JzXrdksVmtnJttkVAfsQQYOIhEP9IcDRilsJ4gA=
https://blog.jermdavis.dev/posts/2015/my-favourite-confusing-exception
If you override "==" you're required to override "!=" too. Custom value types are a good example of where you'd do stuff like this.