Why String.Equals is returning false?

前端 未结 3 686
傲寒
傲寒 2020-12-10 01:25

I have the following C# code (from a library I\'m using) that tries to find a certificate comparing the thumbprint. Notice that in the following code both mycert.Thumb

3条回答
  •  旧巷少年郎
    2020-12-10 01:54

    CompareTo ignores certain characters:

    static void Main(string[] args)
    {
        var a = "asdas"+(char)847;//add a hidden character
        var b = "asdas";
        Console.WriteLine(a.Equals(b)); //false
        Console.WriteLine(a.CompareTo(b)); //0
        Console.WriteLine(a.Length); //6
        Console.WriteLine(b.Length); //5
    
       //watch window shows both a and b as "asdas"
    }
    

    (Here, the character added to a is U+034F, Combining Grapheme Joiner.)

    Debug mode

    So CompareTo's result is not a good indicator of a bug in Equals. The most likely reason of your problem is hidden characters. You can check the lengths to be sure.

    See this for more info.

提交回复
热议问题