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

  • SEARCH
  • Home
  • 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 7438187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:30:52+00:00 2026-05-29T10:30:52+00:00

If I do the overloading of operator == for a class, I must perform

  • 0

If I do the overloading of operator == for a class, I must perform some checks before comparing the fields:

  • if both arguments are null, or both arguments are the same instance, then returns true

    Example: if (System.Object.ReferenceEquals(arg1, arg2)) return true;

  • if one is null, but not both, then returns false

    Example: if (((object)arg1 == null) || ((object)arg2 == null)) return false;

Indeed, if I have a struct and I want to do the overloading of operator ==, these checks are not necessary, rather they are useless, for the following reasons: a struct is a value type, so it can not be null, for example DateTime date = null; is not valid, because DateTime (that is a struct) is not a reference type, so you can not compare two DateTime, one of which is set to null.

I created a simple struct Point2D with operator ==, then I compare an instance of Point2D with null:

Point2D point = new Point2D(0,0);
Console.WriteLine((point == null));
  1. Obviously the operator == it is not called, but the comparison returns False. Which method is called?

  2. The documentation states that overloading this operator in not-immutable types is not recommended. Why?

  • 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-29T10:30:53+00:00Added an answer on May 29, 2026 at 10:30 am

    Because it seems that the compiler optimizes this away. I tried this code:

    System.Drawing.Point point = new System.Drawing.Point(0,0);
    Console.WriteLine((point == null));
    

    And it generated the following IL:

    IL_0000:  ldloca.s    00 
    IL_0002:  ldc.i4.0    
    IL_0003:  ldc.i4.0    
    IL_0004:  call        System.Drawing.Point..ctor
    IL_0009:  ldc.i4.0    
    IL_000A:  call        System.Console.WriteLine
    

    This ultimately boils down to “Create a Point, and then write false to the command line”

    This also explains why it doesn’t call your operator. A struct can never be null, and in circumstances where the compiler can guarantee that you are always going to get false as a result, it doesn’t bother issuing code to call an operator at all.

    The same thing happens with this code, even though String is a class and overload the == operator:

    System.Drawing.Point point = new System.Drawing.Point(0,0);
    Console.WriteLine("foo" == null);
    

    As for immutability… The == operator in C# is generally interpreted to mean “reference equality”, e.g. these two variables point to the same instance of the class. If you are overloading it, then you generally mean to say that two instances of the class, while not the same instance, should behave as if they were the same instance when their data is the same. The classic example is Strings. "A" == GiveMeAnA() even though the actual String reference returned by GiveMeAnA may not be the same as the one represented by the literal "A".

    If you overloaded the == operator on classes which were not immutable, then mutation of a class after == had been evaluated could cause numerous subtle bugs.

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

Sidebar

Related Questions

When overloading assignment operator of a class in C++, must its parameter be reference?
I'm having some issues with implementing a logarithm class with operator overloading in C++.
I have a class using operator overloading, but there are some warnings. // base.h
C++ allows overloading operator new - both global and per-class - usual operator new
I am overloading operator << to implement a stream like interface for a class:
is it possible to construct variadic arguments for function by overloading operator comma of
Possible Duplicate: Java operator overload In c++, we can perform the operator overloading. But
I just got a seg fault in overloading the assignment operator for a class
What is the difference between overloading the operator = in a class and the
I want to implement operator * overloading INSIDE my class, so I would be

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.