C#String:为什么字符串a == b运算符给出的答案不同于a.CompareTo(b)== 0?

nik*_*tix 2 c# string comparison

我用C#搞砸了一下,发现了一个非常不舒服的代码:

static void Main(string[] args)
    {
        string a = "string", b = "string\0";
        bool b1 = a == b;
        bool b2 = (a.CompareTo(b) > 0);
        bool b3 = (a.CompareTo(b) < 0);
        bool b4 = (a.CompareTo(b) == 0);
        Console.WriteLine(a);
        Console.WriteLine(b);
        Console.WriteLine("{0} {1} {2} {3}", b1, b2, b3, b4);
    }
Run Code Online (Sandbox Code Playgroud)

输出:

string
string
False False False True
Run Code Online (Sandbox Code Playgroud)

预期产出(上):

string
string
True False False True
Run Code Online (Sandbox Code Playgroud)

Cha*_*ger 8

结果CompareTo并不意味着平等,它与排序顺序有关.我不确定为了排序目的而忽略null字符太令人惊讶了.

根据文件:

字符集包括可忽略的字符.CompareTo(String)方法在执行区域性敏感比较时不考虑此类字符.