При сравнении строк в C# в чем разница между выполнением
string test = "testvalue";
test.Equals("TESTVALUE", StringComparison.CurrentCultureIgnoreCase);
а также
string test = "testvalue";
test.Equals("TESTVALUE", StringComparison.InvariantCultureIgnoreCase);
... и важно ли вообще включать этот дополнительный параметр?





Microsoft дает достойное руководство, когда использовать свойство InvariantCulture:
MSDN: свойство CultureInfo.InvariantCulture
... an application should use the invariant culture only for processes that require culture-independent results, such as formatting and parsing data that is persisted to a file. In other cases, it produces results that might be linguistically incorrect or culturally inappropriate.
Security Considerations
If a security decision will be made based on the result of a string comparison or case change, your application should use an ordinal comparison that ignores case instead of using InvariantCulture. [...]
String Operations
If your application needs to perform a culture-sensitive string operation that is not affected by the value of CurrentCulture, it should use a method that accepts a CultureInfo parameter. [...]
Persisting Data
The InvariantCulture property is useful for storing data that will not be displayed directly to users. Storing data in a culture-independent format guarantees a known format that does not change. When users from different cultures access the data, it can be formatted appropriately based on specific user. [...]
В других сообщениях есть хорошие советы, но я подумал, что было бы неплохо показать пример того, где это определенно имеет значение:
using System;
using System.Globalization;
using System.Threading;
class Test
{
static void Main()
{
CultureInfo turkish = CultureInfo.CreateSpecificCulture("tr");
Thread.CurrentThread.CurrentCulture = turkish;
// In Turkey, "i" does odd things
string lower = "i";
string upper = "I";
Console.WriteLine(lower.Equals(upper,
StringComparison.CurrentCultureIgnoreCase));
Console.WriteLine(lower.Equals(upper,
StringComparison.InvariantCultureIgnoreCase));
}
}
(Несомненно, много других случаев - это был только первый, о котором я подумал.)
Подробнее о природе турецкого языка как «каноническом падеже», упомянутом конфигуратором, см .: moserware.com/2008/02/does-your-code-pass-turkey-test.html
То же самое встречал на вьетнамском String.Compare("logid", "logId", StringComparison.CurrentCultureIgnoreCase) == 1. Но String.Compare("id", "Id", StringComparison.CurrentCultureIgnoreCase) == 0: O
Да, турецкий i - особый случай. У них есть строчная буква «ı» без точки с заглавной «I» и строчная «i» с заглавной «İ». Это считается каноническим случаем культурных различий.