This fails
string temp = () => {return "test";};
with the error
Cannot convert lambda expression to type ‘string’ because it is not a delegate type
What does the error mean and how can I resolve it?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The problem here is that you’ve defined an anonymous method which returns a
stringbut are trying to assign it directly to astring. It’s an expression which when invoked produces astringit’s not directly astring. It needs to be assigned to a compatible delegate type. In this case the easiest choice isFunc<string>This can be done in one line by a bit of casting or using the delegate constructor to establish the type of the lambda followed by an invocation.
Note: Both samples could be shorted to the expression form which lacks the
{ return ... }