[英]Why does the C# compiler say that string does not have a `Contains` method
為什么C#編譯器說該字符串沒有Contains
方法?
errors
對象是List<Error>
Error.Message
是string
對於此聲明
Assert.True(errors.Any(e => e.Message.Contains("hash value",
StringComparison.OrdinalIgnoreCase)));
編譯器說:
'string'不包含'Contains'的定義和最佳擴展方法重載
System.Linq.Queryable.Contains<TSource>(System.Linq.IQueryable<TSource>, TSource, System.Collections.Generic.IEqualityComparer<TSource>)
參數無效
對於此語句,編譯器很高興:
Assert.True(errors.Any(e => e.Message.IndexOf("hash value",
StringComparison.OrdinalIgnoreCase) >= 0));
C#編譯器是否對使用哪個
Contains
感到困惑,還是我?
你是。
正確的方法是IndexOf()
,而不是Contains()
。 只有一個string.Contains()
重載(如果可以調用它的話),並且不需要StringComparison
參數 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.