Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error CS1660: Cannot convert lambda expression to type 'bool' because it is not a delegate type

This has been driving me mad for ages. I took the following piece of working code

<input @onchange="@((ui) => Console.WriteLine(ui.Value))" />

and copied it to a new blazor component project. I then started getting the error

error CS1660: Cannot convert lambda expression to type 'bool' because it is not a delegate type

like image 689
Sprotty Avatar asked Dec 10 '19 10:12

Sprotty


2 Answers

As this lost me quite a lot of time and the error is very misleading I thought I'd write it up.

The issue is a missing @using add the following in and it all works.

@using Microsoft.AspNetCore.Components.Web

I'm guessing there is some extension method in that namespace that makes the magic work in the razor generated code...

like image 189
Sprotty Avatar answered Nov 15 '22 20:11

Sprotty


Adding the @using statement unfortunately didn't work for me. Apparently, you have to define the lambda expression directly inside the quotes now, without wrapping it inside @(...), like this:

<input @onchange="changeEventArgs => Console.WriteLine(changeEventArgs.Value)" />

See this reference for examples: https://learn.microsoft.com/en-us/dotnet/architecture/blazor-for-web-forms-developers/components#event-handlers

like image 43
Sander Avatar answered Nov 15 '22 20:11

Sander