Search code examples
c#comparisonequals

Equals() method not recognizing similar/same characters when comparing


Why comparing characters with .Equals always returns false?

char letter = 'a';
Console.WriteLine(letter.Equals("a")); // false

Overall I'm trying to write an English - Morse Code translator. I run into a problem comparing char values which shown above. I began with a foreach to analyze all the characters from a ReadLine() input, by using the WriteLine() method, all the characters were transposed fine, but when trying to compare them using the .Equals() method, no matter what I did, it always output false when trying to compare chars.

I have used the .Equals() method with other strings successfully, but it seems to not work with my chars.

using System;
public class MorseCode {
  public static void Main (string[] args) {
    Console.WriteLine ("Hello, write anything to convert it to morse code!");
    var input = Console.ReadLine();
    foreach (char letter in input) {
      if(letter.Equals("a")) {
        Console.WriteLine("Its A - live");
      }
      Console.WriteLine(letter);
    }
    var morseTranslation = "";
    foreach (char letter in input) {
      if(letter.Equals("a")) {
       morseTranslation += ". _ - ";
      }
      if(letter.Equals("b")) {
       morseTranslation += "_ . . . - ";
      }
      if(letter.Equals("c")) {
       morseTranslation += "_ . _ . - ";
      }
      ...
      }
    }
    Console.WriteLine("In morse code, " + input + " is '"morseTranslation + "'");
  }
}

At the beginning, I wrote the foreach to test if it recognized and ran the correct output, but in the end, when I wrote "sample" into the ReadLine(), it gave me :

Hello, write anything to convert it to morse code!
sample
s
a
m
p
l
e

Solution

  • It's important to remember that whilst the char and string keywords look reminiscant of eachother when looking at printed values you should note that they are not accomodated for in exactly the same way.

    When you check a string you can use:

    string s = "A";
    if(s.Equals("A"))
    {
        //Do Something
    }
    

    However, the above will not work with a char. The difference between chars (value types) and strings (reference types) on a surface level is the use of access - single quote (apostrophe) vs quote.

    To compare a char you can do this:

    char s = 'A';
    if(s.Equals('A'))
    {
        //Do Something
    }
    

    On a point relevant to your specific case however, morse code will only requre you to use a single case alphabet and as such when you try to compare against 'A' and 'a' you can call input.ToLower() to reduce your var (string) to all lower case so you don't need to cater for both upper and lower case alphabets.

    It's good that you're aware of string comparissons and are not using direct value comparisson as this:

    if (letter == 'a')
    {
        Console.WriteLine("Its A - live");
    }
    

    Would've allowed you to compare the char but it's bad practice as it may lead to lazy comparisson of strings in the same way and this:

    if (letter == "a")
    {
        Console.WriteLine("Its A - live");
    }
    

    Is a non-representitive method of comparison for the purpose of comparing strings as it evaluates the reference not the direct value, see here