John Kaster
John Kaster

Reputation: 2607

Why does the C# compiler say that string does not have a `Contains` method

Why does the C# compiler say that string does not have a Contains method?

For this statement

Assert.True(errors.Any(e => e.Message.Contains("hash value",
    StringComparison.OrdinalIgnoreCase)));

the compiler says:

'string' does not contain a definition for 'Contains' and the best extension method overload System.Linq.Queryable.Contains<TSource>(System.Linq.IQueryable<TSource>, TSource, System.Collections.Generic.IEqualityComparer<TSource>) has some invalid arguments

And for this statement, the compiler is happy:

Assert.True(errors.Any(e => e.Message.IndexOf("hash value", 
    StringComparison.OrdinalIgnoreCase) >= 0));

Upvotes: 0

Views: 1131

Answers (1)

BoltClock
BoltClock

Reputation: 724222

Is the C# compiler getting confused about which Contains to use, or am I?

You are.

The right method is IndexOf(), not Contains(). There is only one string.Contains() overload (if you could call it that), and it doesn't take a StringComparison parameter.

Upvotes: 4

Related Questions