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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:35:17+00:00 2026-05-30T07:35:17+00:00

A strange piece of code I’ve just discovered in C# (should also be true

  • 0

A strange piece of code I’ve just discovered in C# (should also be true for other CLI languages using .NET’s structs).

using System;

public class Program
{
    public static void Main(string[] args)
    {
    int a;
    long b;

    a = 0;
    b = 0;

    Console.WriteLine(a.Equals(b)); // False
    Console.WriteLine(a.Equals(0L)); // False
    Console.WriteLine(a.Equals((long)0)); // False
    Console.WriteLine(a.Equals(0)); // True
    Console.WriteLine(a.Equals(a)); // True
    Console.WriteLine(a == b); // True
    Console.WriteLine(a == 0L); // True

    Console.WriteLine();

    Console.WriteLine(b.Equals(a)); // True
    Console.WriteLine(b.Equals(0)); // True
    Console.WriteLine(b.Equals((int)0)); // True
    Console.WriteLine(b.Equals(b)); // True
    Console.WriteLine(b == a); // True
    Console.WriteLine(b == 0); // True
    }
}

Two interesting points here (assuming that a is int and b is long):

  1. a != b, but b == a;
  2. (a.Equals(b)) != (a == b)

Is there any reason why comparison was implemented this way?

Note: .NET 4 was used if it makes any difference.

  • 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-30T07:35:19+00:00Added an answer on May 30, 2026 at 7:35 am

    In general, Equals() methods are not supposed to return true for objects of different types.

    a.Equals(b) calls int.Equals(object), which can only return true for boxed Int32s:

    public override bool Equals(Object obj) { 
        if (!(obj is Int32)) {
            return false;
        }
        return m_value == ((Int32)obj).m_value; 
    }  
    

    b.Equals(a) calls long.Equals(long) after implicitly converting the int to a long.
    It therefore compares the two longs directly, returning true.

    To understand more clearly, look at the IL generated by this simpler example (which prints True False True):

    int a = 0;
    long b = 0L;
    
    Console.WriteLine(a == b);
    Console.WriteLine(a.Equals(b));
    Console.WriteLine(b.Equals(a));
    

    IL_0000:  ldc.i4.0    
    IL_0001:  stloc.0     
    IL_0002:  ldc.i4.0    
    IL_0003:  conv.i8     
    IL_0004:  stloc.1     
    
    IL_0005:  ldloc.0     //Load a
    IL_0006:  conv.i8     //Cast to long
    IL_0007:  ldloc.1     //Load b
    IL_0008:  ceq         //Native long equality check
    IL_000A:  call        System.Console.WriteLine    //True
    
    IL_000F:  ldloca.s    00            //Load the address of a to call a method on it
    IL_0011:  ldloc.1                   //Load b
    IL_0012:  box         System.Int64  //Box b to an Int64 Reference
    IL_0017:  call        System.Int32.Equals
    IL_001C:  call        System.Console.WriteLine    //False
    
    IL_0021:  ldloca.s    01  //Load the address of b to call a method on it
    IL_0023:  ldloc.0         //Load a
    IL_0024:  conv.i8         //Convert a to Int64
    IL_0025:  call        System.Int64.Equals
    IL_002A:  call        System.Console.WriteLine    //True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw a strange behavior in a piece of code I wrote under Linux,
Ok, this piece of code may seem strange, but it's the only way I
I've just written a piece of code to display a UIActionSheet within my app.
Ok, here is the piece of code where I found it strange: printf(di consumerthread
I've found a strange looking piece of code in a project I have to
I observed a strange problem in a piece of code where an adhoc SQL
I am working with a piece of code that I find kinda strange. The
I am having a strange issue. I have a very simple piece of code
I have this strange problem when I execute this piece of code: cdrFiles =
i'm having a very strange behaviour with this piece of code: <ui:fragment rendered=#{price.guestIdTrue}> <b>PRICE_GUEST_ID_TRUE

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.