0

I have two strings that i want to compare.

A is a Silverlight user control with the property Header.

B is a plain System.String.

When i compare like so:

if(A.Header == B)

I’m getting that they are not the same.

If I inspect the values in VS2010 with quick watch the values are the same. If I run GetType on both the objects, I find they are both System.String.

I know that i can just compare them with String.Compare.

I though that doing == on strings would always compare the values. Is there something a bit weird with this Silverlight control I am using? Could anyone explain what I am missing here?

Thanks.

Ani
  • 111,048
  • 26
  • 262
  • 307
Jonathan D
  • 1,364
  • 2
  • 12
  • 30

5 Answers5

1

I found the answer it looks like the equals has been overrided in the silverlight control i am using.

thanks to john in this thread for giving me the answer

Are string.Equals() and == operator really same?

Community
  • 1
  • 1
Jonathan D
  • 1,364
  • 2
  • 12
  • 30
0

Do they have same length ? Maybe there is trailing or leading space.

gor
  • 11,498
  • 5
  • 36
  • 42
0

Try this:

char[] arrayA = A.Header.ToCharArray();
char[] arrayB = B.ToCharArray();

and inspect them with VS. It should appear clear where they differ.

Simone
  • 11,655
  • 1
  • 30
  • 43
  • This is **not correct**. The `==` operator is overloaded for `String` types to test for value equality, rather than reference equality. It has generally identical meaning to the `==` operator. See [this question](http://stackoverflow.com/questions/3678792/c-are-string-equals-and-operator-really-same) for an overview of the nuances. – Cody Gray - on strike Feb 01 '11 at 10:47
  • That's not true. Operator == is implemented using Equals() method and compares both sides using theirs values. See http://msdn.microsoft.com/en-us/library/system.string.op_equality.aspx – Nenad Dobrilovic Feb 01 '11 at 10:47
  • Removed downvote in response to your edit. I'll leave my comment just for clarification to anyone else who might think the same thing. – Cody Gray - on strike Feb 01 '11 at 11:06
  • @Cody ok. Thank you & Nenad for the infos, I was really wrong on that! – Simone Feb 01 '11 at 11:08
  • You're welcome. We're all just here to learn something (or at least I am!). And it's a common mistake, since that's how every other reference types work. `String` is a bit of an odd-ball in that (and many other regards). – Cody Gray - on strike Feb 01 '11 at 11:10
0

They might have trailing space or something that looks the same, but has different actual character codes. Like a Cyrillic character е might look like Latin e, but they are not the same. Try to iterate over the characters and see if they all the same.

detunized
  • 15,059
  • 3
  • 48
  • 64
0

Might be leading or trailing spaces, difference in casing, maybe it contains characters that look the same, but have a different character code.

Try the following:

if (string.Compare(A.Header.Trim(), B.Trim(), StringComparison.OrdinalIgnoreCase) == 0)
{
  ..
}
Yannick Motton
  • 34,761
  • 4
  • 39
  • 55
  • A more appropriate way to compare the strings would be with the static [string.Compare(a, b, compare)](http://msdn.microsoft.com/en-us/library/e6883c06.aspx) method. – slugster Feb 01 '11 at 11:03