1

I've wrote this small program in C#

private void Form1_Load(object sender, EventArgs e)
{
    MessageBox.Show(("7797302D875A8922EBFC7DECBD352FE88F35642F" == "‎7797302D875A8922EBFC7DECBD352FE88F35642F").ToString());

    var a = "7797302D875A8922EBFC7DECBD352FE88F35642F";
    var b = "7797302D875A8922EBFC7DECBD352FE88F35642F";
    MessageBox.Show((a == b).ToString());

}

First messageBox shows "False" while Messagebox shows "True".

My question is: why can I not compare the two strings with the == operator?

5
  • 3
    Probably a copy&paste error. Commented Jan 20, 2015 at 12:13
  • possible duplicate of Are string.Equals() and == operator really same? Commented Jan 20, 2015 at 12:15
  • possible duplicate of C# .Equals(), .ReferenceEquals() and == operator Commented Jan 20, 2015 at 12:16
  • 5
    The == operator works just fine for strings. It's your second string literal that contains an invisible character at position 0, so it differs from the first string. Commented Jan 20, 2015 at 12:18
  • 1
    Also "7797302D875A8922EBFC7DECBD352FE88F35642F".Length == 40 && "‎7797302D875A8922EBFC7DECBD352FE88F35642F".Length == 41 Commented Jan 20, 2015 at 12:19

2 Answers 2

10

Your second string has invisible Left-to-right mark character as (U+200E).

Looks like just another copy-paste issue.

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

It might be important to note that you actually can use the == operator for strings.
@TimSchmelter Exactly. I didn't mentioned in my answer because this question already linked/closed as duplicate with stackoverflow.com/q/3678792/447156 and stackoverflow.com/q/814878/447156 in comments section. Also he can check it from reference source as well about how == overloaded for strings.
Thank you for your answer. Some how I didn't think of that my self :D
3

The difference isn't caused by the comparison, but your test string strings.

The second string of the first case starts with the invisible 0x200E, the unicode left-to-right mark.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.