0

I want to compare two char arrays to see if their contents are a palindrome.

static bool isPalindrome(string s)
        {
            char[] normal = s.ToCharArray();
            char[] reversed = s.ToCharArray();
            Array.Reverse(reversed);
            return (normal == reversed) ? true : false;
        }

The problem is that this method never returns true. Even when the arrays contains a palindrome.

What's wrong here?


Edit:

Answer: An array is a referense type. I compared the referenses, not the content.

Enumerable.SequenceEqual(normal, reversed);

Gustav Blomqvist
  • 161
  • 1
  • 12
  • 3
    Arrays are reference types, so are compared by reference (ie are they the same object), `normal` and `reversed` are different objects so don't match. – David Arno Jul 03 '15 at 13:01
  • arrays are reference types in c#. You can't compare the contents with the == sign. – Dave3of5 Jul 03 '15 at 13:01
  • http://stackoverflow.com/questions/4423318/how-to-compare-arrays-in-c, http://stackoverflow.com/questions/14960191/in-c-why-equals-method-on-arrays-only-compare-their-references-not-their-ac, and so on. Search the web for "C# compare arrays" or see duplicate. – CodeCaster Jul 03 '15 at 13:02
  • @Dave3of5: Well you can, it's just that it compares the references, not the contents of the arrays. – Jon Skeet Jul 03 '15 at 13:02
  • @JonSkeet I'll edit to be more clear. On a note might make these comments seem a bit strange... – Dave3of5 Jul 03 '15 at 13:05
  • As this has been shut already, I can't post my handy answer, so I'll put it here. You can check for a palindrome by replacing the contents of your method with `return if (s != null && s == string.Concat(s.Reverse()));` – David Arno Jul 03 '15 at 13:08
  • @CodeCaster, will you please stop closing questions without letting others agree/disagree first. Comparing arrays *is not* the answer to this question, so it's not a duplicate of that question. – David Arno Jul 03 '15 at 13:11
  • @David your suggestion is a total rewrite of OP's code. OP's question as it currently stands is _"How to compare array contents"_ (or rather _"Why does comparing arrays not compare their contents"_), which is answered in the duplicate I selected, and also in the two links I posted. I also do check up on questions I close as duplicate to see whether OP or anyone else disagrees with me in the comments. That rarely happens, but if it does (and if I agree) I reopen just as fast. – CodeCaster Jul 03 '15 at 13:13

1 Answers1

3

You're comparing the two arrays together, not the contents of the arrays. So essentially you're comparing two memory locations and asking "Are they the same?".

Think of it like this, you've got 2 buckets and you put one set of socks in one bucket, the other from the pairs in the other bucket. Are those 2 buckets the same? No... despite the fact that they contents may be.

For completeness checkout this question to answer how to achieve the comparison you're aiming for Easiest way to compare arrays in C#

Community
  • 1
  • 1
Ian
  • 33,605
  • 26
  • 118
  • 198
  • How do i easily compare their contents? – Gustav Blomqvist Jul 03 '15 at 13:03
  • @GustavBlomqvistGurra Checkout the answer that someones linked to in the close vote. Should explain how to do it. – Ian Jul 03 '15 at 13:04
  • @DavidArno - by the way, I'd contest before the edit it was an answer... Just a very short one while I was elaborating. – Ian Jul 03 '15 at 13:05
  • @Ian, I agree; that's why I removed the comment and the downvote :) – David Arno Jul 03 '15 at 13:07
  • @DavidArno - ah thanks. Assumed the downvote was removed only after the edit. Appreciate you taking the time to re-consider! :) – Ian Jul 03 '15 at 13:07
  • @Ian, it bugs me when people downvote then move on. So if I downvote and/or vote to close I try to check back to cancel them if good edits have been made. – David Arno Jul 03 '15 at 13:14