C# 无法将方法组分配给隐式类型的局部变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19623299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot assign method group to an implicitly-typed local variable
提问by XXXX
I have this error
我有这个错误
"Cannot assign method group to an implicitly-typed local variable"
“无法将方法组分配给隐式类型的局部变量”
in this code
在这段代码中
private async void Button_Click_2(object sender, RoutedEventArgs e)
{
var frenchvoice = InstalledVoices.All.Where(voice => voice.Language.Equals("fr-FR") & voice.Gender == VoiceGender.Female).FirstOrDefault; // in this line
sp.SetVoice(frenchvoice);
await sp.SpeakTextAsync(mytxt);
}
回答by SLaks
You forgot to call the function (with ()
)
您忘记调用该函数(使用()
)
回答by Steve
You must add the brackets to call the method FirstOrDefault
您必须添加括号才能调用该方法 FirstOrDefault
var frenchvoice = InstalledVoices.All
.Where(voice => voice.Language.Equals("fr-FR") &&
voice.Gender == VoiceGender.Female)
.FirstOrDefault();
And, while your code works also using the & operator, the correct one to use in a logical condition is &&
而且,虽然您的代码也使用 & 运算符工作,但在逻辑条件中使用的正确代码是 &&
By the way, FirstOrDefault
accepts the same lambda applied for Where so you could reduce your code to a simpler and probably faster
顺便说一句,FirstOrDefault
接受应用于 Where 的相同 lambda,因此您可以将代码简化为更简单且可能更快
var frenchvoice = InstalledVoices.All
.FirstOrDefault(voice => voice.Language.Equals("fr-FR") &&
voice.Gender == VoiceGender.Female);