In VB.NET, a lambda expression can be declared and invoked on the same line:
'Output 3
Console.WriteLine((Function(num As Integer) num + 1)(2))
Is this possible in C#?
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.
You have to tell the compiler a specific delegate type. For example, you could cast the lambda expression:
EDIT: Or yes, you can use a delegate creation expression as per Servy’s answer:
Note that this isn’t really a normal constructor call – it’s special syntax for delegate creation which looks like a regular constructor call. Still clever though 🙂
You can make it slightly cleaner with a helper class:
… then:
Or: