Consider a scenario where you are implementing code meant to be used for machine to machine communication. The typical example for that is the code executed inside a web API action method.
Suppose that you want to perform an exact match between strings; maybe you have a list of users and you wan to find one specific user provided the user name:
List<User> users = ....
const string username = "user-123";
var user = users.Find(u => string.Equals(username, u.UserName));
In such a scneario should I use StringComparison.Ordinal
or StringComparison.InvariantCulture
?
Based on my understanding, since I want to perform an exact match between two strings, the proper choice here is StringComparison.Ordinal
.
The typical example for culture sensitive comparison between the two strings Straße
and strasse
, where the two strings are considered equal due to linguistical rules, does not seem to fit here.
Is this assumption correct ?
If this is correct, can you provide an example where using the invariant culture instead of the ordinal comparison is the right choice ?
Just to clarify, I'm asking because I'm working on a code base where there are plenty of string comparisons using the invariant culture. Many of these cases refer to exact string match performed in a machine to machine communication scenario. So I want to be sure to clearly understand the rationale behind the right choice of a string comparison value.