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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:49:20+00:00 2026-06-06T21:49:20+00:00

When I compare two variables typed as object and both contain same value, the

  • 0

When I compare two variables typed as object and both contain same value,
the comparison result using == operator produces false.

object Var1 = "X";
object Var2 = "X";

// This produces false result
bool Match = Var1 == Var2;

Why is this happening?

Edit: Above is the code that actually works!

I have based it on my real code which looks
like this and does not work:

ChoiceValue = Choice.GetValue(FieldTemplate.ValueDataType);
if (ChoiceValue == Field.Value) RadioButton.IsChecked = true;

ChoiceValue is object and also the Field.Value is property typed as object.

Obviously works differently in different situations.

  • 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-06-06T21:49:21+00:00Added an answer on June 6, 2026 at 9:49 pm

    The reason this specific case returns false is because your strings are not interned. (String interning)

    When I tested it, I got true, because my strings were interned.

    In your case, this causes the object == operator to return false, since it compares by reference.

    The reason your strings are not interned is because you are comparing dynamically built strings (meaning they were not known at compile time, but at runtime).

    If you absolutely must use object variables, you can use Equals instead of ==, or you can manually intern strings with String.Intern

    This case is an anomaly of the reference-typed strings trying to behave like value types. This means that they compare by value, when using the string == operator. However, when you have objects, it uses the object == operator, which compares by reference.

    This is explained in the documentation for string.

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

Sidebar

Related Questions

Overloading the comparison operator, how to compare if the two variables points to the
In JSP, I want to compare two variables If I do: <c:set var=pagerTotDisp value=9/>
In Erlang, using => to compare two variables results in a syntax error, you
I am trying to compare two variables in which are usually strings. These variables
I have two variables: unsigned short a,b; /* When I compare them with a
I would like to compare two columns in the same table. I want to
I want to compare to variables, both of type T extends Number . Now
I'm struggling to compare two php variables to display or not display some text
I'm trying to compare two variables of type nullable(of boolean) in VB.NET 2010. One
I am trying to compare the addresses of two variables in memory: chunk_t *old_chunk,

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.