Just few moments ago i learned some thing on Stackoverflow, hence i thought of sharing the same with you. For comparing strings with culture specific, we normally use String.Compare. But very rarely people would have seen other API available in .NET FCL called CultureInfo.Compare().
So i started to dig in and saw whats actually the difference between them, infact they do work the same. Upon looking inside the IL i came to know that string.Compare infact does call or uses CultureInfo.Compare() API.
public static int Compare(string strA, string strB, CultureInfo culture, CompareOptions options)
if (culture == null)
throw new ArgumentNullException(“culture”);
return culture.CompareInfo.Compare(strA, strB, options);
So following are my observations done further on this:
- String.Compare first checks if input is null i.e CultureInfo argument.
- CultureInfo.Compare does not check for null except at later stage it does check for string arguments but not for CultureInfo.
- CultureInfo.Compare API internally calls String.Compare() API again if OrdinalIgnoreCase is passed for CompareOptions argument.
So based on above points, its best to use String.Compare API itself, because it first does null checking and String class is a BCL class. I believe it also avoids confusions for next developers looking at your code if you’re using some alternative in this case CultureInfo.Compare API().
That’s all for now, your comments are always welcome.