Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Error CA1307 with string.Contains(string, System.StringComparison)?

Tags:

c#

.net-core

In my test I have assertion that contains p.Contains(".Tests") but it makes error

Error CA1307 The behavior of 'string.Contains(string)' could vary based on the current user's locale settings. Replace this call in 'Solution.Tests.PackageTests._bowerEnabledProjects' with a call to 'string.Contains(string, System.StringComparison)'.

So I wanted to fix it by: p.Contains(".Tests", System.StringComparison) but it makes error:

Error CS0119 'StringComparison' is a type, which is not valid in the given context

like image 500
DiPix Avatar asked Feb 17 '26 12:02

DiPix


1 Answers

StringComparison is an enum - the warning suggests that you're meant to specify one of the values within that enum, e.g. StringComparison.Ordinal.

However, this warning is wrong on two counts:

  • There is no string.Contains(string, StringComparison) method as far as I can see. The documentation for string.Contains even gives an example of how you would implement such a method.
  • The warning's claim that the behavior depends on the locale settings is incorrect. The documentation for string.Contains states: "This method performs an ordinal (case-sensitive and culture-insensitive) comparison."

I would suggest disabling the warning.

like image 163
Jon Skeet Avatar answered Feb 20 '26 02:02

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!