C#字符串比较:CurrentCultureIgnoreCase和InvariantCultureIgnoreCase之间的差异 [英] C# String comparisons: Difference between CurrentCultureIgnoreCase and InvariantCultureIgnoreCase
问题描述
在C#中进行字符串比较时,执行
string test =testvalue之间的区别是什么?
test.Equals(TESTVALUE,StringComparison.CurrentCultureIgnoreCase);
和
string test =testvalue;
test.Equals(TESTVALUE,StringComparison.InvariantCultureIgnoreCase);
...并且包含额外的参数很重要吗?
Microsoft为何时使用 InvariantCulture
属性:
MSDN:CultureInfo.InvariantCulture属性
...应用程序应使用
安全注意事项 p>
不变文化仅用于需要与文化无关的
结果的进程
,例如格式化和
解析持久保存到
文件的数据。
如果基于字符串
比较或大小写更改的结果做出安全决定
,则您的
应用程序应使用序数
比较,忽略使用InvariantCulture的情况而不是
。 [...]
字符串操作 a
文化敏感的字符串操作
不受
CurrentCulture的值的影响,它应该使用接受CultureInfo参数的方法
。
[...]
持久化数据
属性是
用于存储数据,不会直接显示给用户
。
以与文化无关的
格式存储数据,保证了
不会改变的已知格式。当用户从
不同的文化访问数据时,
可以根据特定用户的
格式化。 [...]
When doing a string comparison in C#, what is the difference between doing a
string test = "testvalue";
test.Equals("TESTVALUE", StringComparison.CurrentCultureIgnoreCase);
and
string test = "testvalue";
test.Equals("TESTVALUE", StringComparison.InvariantCultureIgnoreCase);
... and is it important to include that extra parameter, anyway?
Microsoft gives some decent guidance for when to use the InvariantCulture
property:
MSDN: CultureInfo.InvariantCulture Property
... 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. [...]
这篇关于C#字符串比较:CurrentCultureIgnoreCase和InvariantCultureIgnoreCase之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!