问题
I'm sorry if this has been asked before, I have searched for it but can't find anything that answers my confusion.
If I write the following code, I get a compiler error saying No overload for method 'Contains' takes 2 arguments
, but IntelliSense suggests that there IS an overload that takes 2 arguments (screenshot here):
string s = "Hello";
if (s.Contains('h', StringComparer.OrdinalIgnoreCase))
{
Console.WriteLine("True!");
}
I'm passing a char as first argument and StringComparer
implements IEqualityComparer
so I don't get what is wrong.
Can anyone explain why I get an error?
回答1:
StringComparer implements IEqualityComparer<string>
with type argument string, but the expected parameter is IEqualityComparer<char>
with type argument char.
The types are still incompatible.
Have a read about generic type parameters: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generic-type-parameters
来源:https://stackoverflow.com/questions/49152427/no-overload-takes-2-arguments-but-intellisense-shows-overload-with-2-arguments