“No overload takes 2 arguments” but IntelliSense shows overload with 2 arguments

家住魔仙堡 提交于 2020-01-24 08:59:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!