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 7619401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:40:42+00:00 2026-05-31T03:40:42+00:00

Please see my comment in the code below. How should I check for the

  • 0

Please see my comment in the code below. How should I check for the parameter to be null? It looks like null is being casted to Foo which essentially makes a recursive call the the == operator. Why does this happen?

public class Foo
{
    public static bool operator ==(Foo f1, Foo f2)
    {
        if (f1 == null) //This throw a StackOverflowException
            return f2 == null;
        if (f2 == null)
            return f1 == null;
        else
            return f1.Equals((object)f2);
    }

    public static bool operator !=(Foo f1, Foo f2)
    {
        return !(f1 == f2);
    }

    public override bool Equals(object obj)
    {
        Foo f = obj as Foo;
        if (f == (Foo)null)
            return false;

        return false;
    }

    public override int GetHashCode()
    {
        return 0;
    }
}
  • 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-31T03:40:43+00:00Added an answer on May 31, 2026 at 3:40 am

    Why does this happen?

    Because the language rules say to.

    You’ve provided an operator with this signature:

    public static bool operator ==(Foo f1, Foo f2)
    

    and then – wherever this happens to be in code – you’ve got this expression:

    f1 == null
    

    where f1 has a compile-time type of Foo. Now null is implicitly convertible to Foo as well, so why wouldn’t it use your operator? And if you’ve got the first line of your operator unconditionally calling itself, you should expect a stack overflow…

    In order for this not to happen, you’d need one of the two changes to the language:

    • The language would have to special-case what == meant when it’s used within a declaration for ==. Ick.
    • The language would have to decide that any == expression with one operand being null always meant the reference comparison.

    Neither is particularly nice, IMO. Avoiding it is simple though, avoids redundancy, and adds an optimization:

    public static bool operator ==(Foo f1, Foo f2)
    {
        if (object.ReferenceEquals(f1, f2))
        {
            return true;
        }
        if (object.ReferenceEquals(f1, null) ||
            object.ReferenceEquals(f2, null))
        {
            return false;
        }
        return f1.Equals(f2);
    }
    

    However, you then need to fix your Equals method, because that then ends up calling back to your ==, leading to another stack overflow. You’ve never actually ended up saying how you want equality to be determined…

    I would normally have something like this:

    // Where possible, define equality on sealed types.
    // It gets messier otherwise...
    public sealed class Foo : IEquatable<Foo>
    {
        public static bool operator ==(Foo f1, Foo f2)
        {
            if (object.ReferenceEquals(f1, f2))
            {
                return true;
            }
            if (object.ReferenceEquals(f1, null) ||
                object.ReferenceEquals(f2, null))
            {
                return false;
            }
    
            // Perform actual equality check here
        }
    
        public override bool Equals(object other)
        {
            return this == (other as Foo);
        }
    
        public bool Equals(Foo other)
        {
            return this == other;
        }
    
        public static bool operator !=(Foo f1, Foo f2)
        {
            return !(f1 == f2);
        }
    
        public override int GetHashCode()
        {
            // Compute hash code here
        }
    }
    

    Note that this allows you to only bother with nullity checks in one place. In order to avoid redundantly comparing f1 for null when it’s been called via an instance method of Equals to start with, you could delegate from == to Equals after checking for nullity of f1, but I’d probably stick to this instead.

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

Sidebar

Related Questions

In code below (please see comment): #include stdafx.h #include <iostream> using std::cout; struct Base
Please see code below. The destructors are never called. Anyone know why and how
Please see the code below: #include <iostream> #include <stdlib.h> #include <time.h> using namespace std;
everyone. Please see example below. I'd like to supply a string to 'schedule_action' method
I have an Adorner which adornes a Border (please see screenshot below). The MouseDown
How to take integer in hashlib.sha1(int). Please see the code in which i am
I have a textarea in C#, please see below code: <asp:Label ID=lblQuestions runat=server CssClass=addinfo>
Please see the below code. It shows animation effect with image. UIImageView *imgView =
I'm writing a simple program - please see below for my code with comments.
Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i

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.