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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:52:03+00:00 2026-05-27T08:52:03+00:00

Why does C# allow for the implicit conversion of an object to an int

  • 0

Why does C# allow for the implicit conversion of an object to an int for equality comparison, but not for an object type to object type comparison, even though an implicit operator exists?

The bool errorHere = valueOnly == valuePair; line gives the exception at the bottom.

class Program
{
    static void Main(string[] args)
    {
        ValueOnly valueOnly = new ValueOnly();
        ValuePair valuePair = new ValuePair();
        bool areEqual = valueOnly.Value == valuePair;
        bool errorHere = valueOnly == valuePair;
        bool butNotHere = valueOnly == (ValueOnly)valuePair;
        valueOnly = valuePair; // Or Here
    }
}

public class ValuePair
{
    public int Value { get; set; }
    public string Text { get; set; }

    public static implicit operator ValueOnly(ValuePair valuePair) {
        if (valuePair.Text != null || valuePair.Value != 0) {
            return new ValueOnly() { Value = valuePair.Value };
        }
        return null;
    }

    public static implicit operator int(ValuePair valuePair) {
        return valuePair.Value;
    }
}

public class ValueOnly
{
    public int Value { get; set; }
}

Here is the error:

Error   Operator '==' cannot be applied to operands of type 'ValueOnly' and 'ValuePair'
  • 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-27T08:52:04+00:00Added an answer on May 27, 2026 at 8:52 am

    C# most certainly does not allow implicit conversions of objects to ints for any purpose. I ‘m not sure if you are referring to the line

    bool butNotHere = valueOnly == (ValueOnly)valuePair; 
    

    as “allowing” such a conversion. If so, it does not do any such thing. It simply invokes your own conversion operator (which if I read the code correctly will return null since the value will be a default-constructed integer) and then does a reference comparison between the two objects (which evaluates to false as null is not reference-equal to anything).

    It also goes without saying that once you define an implicit conversion operator C# will use it whenever it is necessary. Therefore I think your question has to do more with understanding how the equality operator works based on the types of its arguments than anything else.

    Update, regarding the valueOnly == valuePair check:

    The C# specification, 7.10.6 (Reference type equality operators) states:

    It is a binding-time error to use the predefined reference type
    equality operators to compare two references that are known to be
    different at binding-time. For example, if the binding-time types of
    the operands are two class types A and B, and if neither A nor B
    derives from the other, then it would be impossible for the two
    operands to reference the same object. Thus, the operation is
    considered a binding-time error.

    If you want to be able to compare objects of class type for equality, the best way is to implement the IEquatable<T> interface, and override object.Equals to be implemented in terms of IEquatable<T>.Equals (this means that you will also have to override object.GetHashCode) to match.

    However, it’s a bad idea to rely on “hidden” conversions when comparing items. If we ‘re talking about comparing unrelated classes, you will save yourself a lot of anguish down the road by making the comparison as prominent as possible. To that end, I would suggest the low tech

    if(valuePair.Value == valueOnly.Value)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

C++ does not allow polymorphism for methods based on their return type. However, when
Firefox 3.5 does not allow creating java OBJECT tag with Javascript (document.write)? this technique
I know VB.Net does not allow implicit interface implementation like C#. And thus code
IE does not allow writing to the innerHTML property of style or head elements.
Since AS3 does not allow private constructors, it seems the only way to construct
if my host does not allow me to upload a file directly to my
The organization policy does not allow normal users be assigned to Administrators group of
So I have a client who's current host does not allow me to use
Is there any html form class(text box) that does not allow null values. The
Why does C++ (and probably C as well) allow me to assign and int

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.