Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 540239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:13:11+00:00 2026-05-13T10:13:11+00:00

Possible Duplicate: C# okay with comparing value types to null I was working on

  • 0

Possible Duplicate:
C# okay with comparing value types to null

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:

if(this.Handle != null)
{
   //BeginInvokeCode
}

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:

if(this.Handle != IntPtr.Zero)
{
   //BeginInvokeCode
}

So then it hit me, why did it even compile when I was checking it for null? So I decided to try it myself:

    public struct Foo { }

and then:

    static void Main(string[] args)
    {
        Foo f = new Foo();
        if (f == null) { }
    }

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:

[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; }
}

This finally compiled:

    static void Main(string[] args)
    {
        Foo f = new Foo();
        if (f == null) { }
    }

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:13:12+00:00Added an answer on May 13, 2026 at 10:13 am

    It looks like the issue is that when MS introduced nullable types, they made it so that every struct is implicitly convertable to its nullable type (foo?), so the code

    if( f == null)
    

    is equivalent to

    if ( (Nullable<foo>)f == (Nullable<foo>)null) 
    

    Since MSDN states that “any user-defined operators that exist for value types may also be used by nullable types”, when you override operator==, you allow that implicit cast to compile, as you now have a user-defined == — giving you the nullable overload for free.

    An aside:

    Seems like in your example, there is some compiler optimization
    The only thing that is emitted by the compiler that even hints there was a test is this IL:

    ldc.i4.0
    ldc.i4.0
    ceq
    stloc.1   //where there is an unused boolean local
    

    Note that if you change main to

    Foo f = new Foo();
    object b = null;
    if (f == b) { Console.WriteLine("?"); }
    

    It no longer compiles. But if you box the struct:

    Foo f = new Foo();
    object b = null;
    if ((object)f == b) { Console.WriteLine("?"); }
    

    if compiles, emits IL, and runs as expected (the struct is never null);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 402k
  • Answers 402k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In the eclipse magazine 03-2010 is an article about that… May 15, 2026 at 4:47 am
  • Editorial Team
    Editorial Team added an answer GAEUnit creates its own proxy stub datastore, using this code… May 15, 2026 at 4:47 am
  • Editorial Team
    Editorial Team added an answer Use ExternalInterface to call a javascript function for it. Sorry… May 15, 2026 at 4:47 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.