31

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);
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
XXXX
  • 319
  • 1
  • 3
  • 3

2 Answers2

76

You forgot to call the function (with ())

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
25

You must add the round brackets to call the method 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 to Where so you could reduce your code to a simpler and probably faster

   var frenchvoice = InstalledVoices.All
       .FirstOrDefault(voice => voice.Language.Equals("fr-FR") && 
                                voice.Gender == VoiceGender.Female);
Steve
  • 213,761
  • 22
  • 232
  • 286