- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道如果使用 String.Equals(string),为什么静态代码分析(VS 2015)不会导致错误。所以
"file".Equals("FILE")
不会导致代码分析错误,而
String.Compare("file", "FILE", true)
确实会导致 CA 1307 错误。
此规则的文档在 MSDN 上说明
Many string operations, most important the Compare and Equals methods, provide an overload that accepts a StringComparison enumeration value as a parameter.
Whenever an overload exists that takes a StringComparison parameter, it should be used instead of an overload that does not take this parameter. By explicitly setting this parameter, your code is often made clearer and easier to maintain.
由此我预计第一个示例会导致 CA 1307 错误。
因此,在某些情况下不抛出错误可能会非常危险,并导致“错误”的应用程序。
我是不是漏掉了什么?
如果这是设计使然,有什么方法可以实现预期的行为?
最佳答案
您传递的比较方法是一个 bool 值,而比较函数需要一个 StringComparison 枚举值。
String.Compare("file", "FILE", StringComparison.InvariantCultureIgnoreCase);
这应该返回一个真实的响应,因为您忽略了大小写。
关于c# - CA1307 : Specify StringComparison not thrown for string. 等于(字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35509327/
我有一些这样的代码: If key.Equals("search", StringComparison.OrdinalIgnoreCase) Then DoSomething() End If
根据我的理解(参见 my other question ),为了决定是使用序数规则还是文化规则来测试字符串相等性,语义必须考虑执行的比较。 如果必须将两个比较的字符串视为原始字符序列(换句话说,两个符
我对 StringComparison Enumeration 的选项感到困惑.我只想比较两个忽略大小写的字符串。谁能解释一下当前文化、不变文化 和序数 是什么意思?是否有大多数用例通用的选项?如果是
给定一个 StringComparison 实例,我应该如何检查它是否区分大小写? 我应该将它与枚举中所有当前已知的区分大小写的值进行比较吗? StringComparison sc = ...; b
做了很多不区分大小写的字符串比较,我以很多冗长的语句结束,比如: myList.FirstOrDefault( c => string.Equals( c.Inter
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Which is generally best to use — StringComparison.Ordi
根据 MSDN:StringComparison.InvariantCulture: Specifies the culture, case, and sort rules to be used by
这段代码是一个简单的词汇-翻译器。我可以使用 StringComparison.CurrentCultureIgnoreCase,以便在您输入 textBox1 时不考虑注册单词吗? using Sy
我正在将 C# 代码移植到 Windows 应用商店应用程序。令我惊讶的是,以下代码不再有效: someString.Equals("someOtherString", StringCompariso
某些 .NET 方法使用 StringComparison作为参数,有些使用 StringComparer (通常采用 IComparer 的形式)。差异是显而易见的。是否有一些优雅的方法如何从 St
我有这段代码: var whackPos = str.IndexOf("/"); ...Resharper 建议更改为: var whackPos = str.IndexOf("/", StringC
我有列表,如何获得不同的值? IList words = new List { "A", "b", "a" }; var distinctWords = words.Distinct(StringCo
string body = Selenium.GetBodyText(); if (body.Contains("software", StringComparison.CurrentCultureI
在 Best Practices for Using Strings in the .NET Framework我们鼓励提供适当的 StringComparison 每当我们比较字符串时。我同意这一点
我的目标是省略author_name 以pipeline 和author_email 以pipeline@gmail.com 如果项目名称以ej2或Ej2开头。 我有以下代码。此代码完全符合我的预期。
我的目标是省略author_name 以pipeline 和author_email 以pipeline@gmail.com 如果项目名称以ej2或Ej2开头。 我有以下代码。此代码完全符合我的预期。
我在代码中有多个实例,我们在这些实例中完成了 StringComparison.CurrentCultureIgnoreCase 以比较两个字符串的相等性。鉴于,- 在我们的代码中,字符串基于单一文化
我正在尝试如下所示进行字符串同情: var nDetails = listOfServiceUrls.Where(x => String.Equals(x.Description, serviceN
我正在根据从客户端浏览器发送的排序字段集合对数据集进行一些自定义筛选和排序,并且正在使用 Dynamic Linq 来实现(大部分)预期效果。我遇到问题的地方是当我尝试按字符串类型的列进行排序时,它包
在 C# 中,您可以使用 String.Equals 比较两个字符串并提供 StringComparison。 我最近一直在寻求更新比较 ToLower() 的陈旧方法,因为我了解到它不适用于所有语言
我是一名优秀的程序员,十分优秀!