0

In other words, can these lines be combined?

Func<int, int> square = x => x * x; Console.WriteLine(square(5)); 

There is no recursion here - I'm not looking for Y combinator, but rather just immediately using the function I defined.

I tried something like this - but could not get around syntax errors

Console.WriteLine( // defining the function inline (new Func<List<int>, string>(list) => string.Join(",", list)) (new List<int>{1,2,3}) // immediately passing the parameter ); 
5
  • These code blocks don't do the same thing. Unclear what your question is. Yes, C# supports anonymous functions... Yes, you can pass arguments to anonymous functions. Yes, you can print the result of calling an anonymous function. Commented May 1, 2023 at 22:24
  • Also, join strings with a function, not a manual loop Commented May 1, 2023 at 22:26
  • it was just an example for formatting Commented May 1, 2023 at 22:29
  • @James please review my edit and updated duplicate - I think I totally misunderstood what you were looking for based on the title... and example actually made sense. I put (slightly shortened) version back... I believe you are after IIFE. Commented May 1, 2023 at 23:02
  • @AlexeiLevenkov Thanks. I had some misplaced parentheses Commented May 2, 2023 at 0:55

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.