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
);
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
James
  • 1
  • 1
  • 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. – OneCricketeer May 01 '23 at 22:24
  • Also, [join strings](https://stackoverflow.com/questions/3610431/string-join-on-a-listint-or-other-type) with a function, not a manual loop – OneCricketeer May 01 '23 at 22:26
  • it was just an example for formatting – James May 01 '23 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. – Alexei Levenkov May 01 '23 at 23:02
  • @AlexeiLevenkov Thanks. I had some misplaced parentheses – James May 02 '23 at 00:55

0 Answers0