我有一個執行各種比較的分揀機。 Resharper說我應該從String.CompareTo
改爲String.CompareOrdinal
。這是真的提供了很多好處,還是我應該調整檢查規則?Resharper建議將CompareTo更改爲CompareOrdinal
的CompareTo:
config.Sort(delegate(configData data1, configData data2)
{
if (data1.SettingName.Equals(data2.SettingName))
{
string m1 = data1.SettingMachine;
string m2 = data2.SettingMachine;
if (m1 == null)
m1 = string.Empty;
if (m2 == null)
m2 = string.Empty;
return m1.CompareTo(m2);
}
return data1.SettingName.CompareTo(data2.SettingName);
});
CompareOrdinal:
config.Sort(delegate(configData data1, configData data2)
{
if (data1.SettingName.Equals(data2.SettingName))
{
string m1 = data1.SettingMachine;
string m2 = data2.SettingMachine;
if (m1 == null)
m1 = string.Empty;
if (m2 == null)
m2 = string.Empty;
return String.CompareOrdinal(m1, m2); ;
}
return String.CompareOrdinal(data1.SettingName, data2.SettingName);
});
警告是「CompareTo是文化特定的」 – 2012-08-13 21:02:33
也相關:http://stackoverflow.com/questions/10941375/could-string-comparisons-really-differ-based-on-culture-when-the-string- is-guara – 2012-08-13 21:03:04
因此,從resharper的角度來看,它似乎是基於保持文化的理念推薦它 – Adam 2012-08-13 21:15:54