Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing structs to null
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/1972262/c-okay-with-comparing-value-types-to-null">C# okay with comparing value types to null</a> </p> </blockquote> <p>I was working on a windows app in a multithreaded environment and would sometimes get the exception "Invoke or BeginInvoke cannot be called on a control until the window handle has been created." So I figured that I'd just add this line of code:</p> <pre><code>if(this.Handle != null) { //BeginInvokeCode } </code></pre> <p>But that didn't solve the problem. So I dug a little further, and realized that IntPtr (the type that Form.Handle is) is a struct which can't be nullable. This was the fix that worked:</p> <pre><code>if(this.Handle != IntPtr.Zero) { //BeginInvokeCode } </code></pre> <p>So then it hit me, why did it even compile when I was checking it for null? So I decided to try it myself:</p> <pre><code> public struct Foo { } </code></pre> <p>and then:</p> <pre><code> static void Main(string[] args) { Foo f = new Foo(); if (f == null) { } } </code></pre> <p>and sure enough it didn't compile saying that "Error 1 Operator '==' cannot be applied to operands of type 'ConsoleApplication1.Foo' and ''". Ok, so then I started looking at the metadata for IntPtr and started adding everything to my Foo struct that was there in the IntPtr struct (ISerializable, ComVisible) but nothing helped. Finally, when I added the operator overloading of == and !=, it worked:</p> <pre><code>[Serializable] [ComVisible(true)] public struct Foo : ISerializable { #region ISerializable Members public void GetObjectData(SerializationInfo info, StreamingContext context) { throw new NotImplementedException(); } #endregion public override bool Equals(object obj) { return base.Equals(obj); } public override int GetHashCode() { return base.GetHashCode(); } public static bool operator ==(Foo f1, Foo f2) { return false; } public static bool operator !=(Foo f1, Foo f2) { return false; } } </code></pre> <p>This finally compiled:</p> <pre><code> static void Main(string[] args) { Foo f = new Foo(); if (f == null) { } } </code></pre> <p>My question is why? Why if you override == and != are you allowed to compare to null? The parameters to == and != are still of type Foo which aren't nullable, so why's this allowed all of a sudden?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload