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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:14:53+00:00 2026-05-26T10:14:53+00:00

when i cast int and float to object and compare them the equality is

  • 0

when i cast int and float to object and compare them the equality is always false. Why?

        float f = 0.0f;
        int i = 0;
        Console.WriteLine(f.Equals(i)); // true
        Console.WriteLine(i.Equals(f)); // false
        Console.WriteLine(i == f); // true
        Console.WriteLine("----------------");
        object obf = f;
        object obi = i;
        Console.WriteLine(obf.Equals(obi)); // false
        Console.WriteLine(obi.Equals(obf)); // false
        Console.WriteLine(obi == obf); // false
        Console.WriteLine("----------------");

Update:
this is NOT the case for the same type

        int i1 = 1;
        int i2 = 1;
        object oi1 = i1;
        object oi2 = i2;
        Console.WriteLine(oi1.Equals(oi2)); // true
        Console.WriteLine(oi2.Equals(oi1)); // true
  • 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-26T10:14:53+00:00Added an answer on May 26, 2026 at 10:14 am

    A float is only equal to another float, and an int is only equal to another int. The only lines which are returning true are these ones:

    Console.WriteLine(f.Equals(i));
    Console.WriteLine(i == f);
    

    In both of these cases, there’s an implicit conversion of the value of i to float, so they’re equivalent to:

    Console.WriteLine(f.Equals((float) i));
    Console.WriteLine((float) i == f);
    

    These conversions are just normal conversions required for overload resolution of methods and operators.

    None of the rest of the lines involve that implicit conversion, so they’re comparing the two different types, which gives a result of false even when it is comparing by value (which is the case with all the Equals calls). That’s why using Equals on the boxed int values does return true, because that’s comparing two values of the same type, by value.

    In this case:

    Console.WriteLine(obi == obf);
    

    it’s not even trying to compare numeric values – it’s comparing the references for the boxed objects. As there are two different references, the result is false – and would be even if both values were of type int.

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

Sidebar

Related Questions

Possible Duplicate: PHP unexpected result of float to int type cast int((0.1+0.7)*10) = 7
I have some no understanding about how one can cast int to float, step
I have tried several ways to cast a float to an int, what I
How do I cast an int to an enum in C#?
Including DISTINCT to an SQL query that also uses ORDER BY CAST(thecolumn AS int)
Can you cast a List<int> to List<string> somehow? I know I could loop through
When I try to use a static_cast to cast a double* to an int*,
I am curious as to why an implicit cast fails in... int? someValue =
I want to cast both MenuItem objects and Button control objects to an object
Hi I'm trying to convert from an int to a float in C and

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.