C# Querying With LINQ Functional Programming in C# Lambda Expressions

luke hammer
luke hammer
25,505 Points

I'm very confused on passing more than one item to a Lamda. Looking for a point in the right dirrection

can more than one variable be put in the => ???

I'm confused about the whole point of actions i guess. This is what i understand actions are like Functions but they do not return a value. It appears in this example that the function should be used to call the function.

if you could provide a working example of some code in the Main that uses these i think i could figure it out from there.

Thank you for your help.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    public class Program
    {     
        public Func<int, int> Square = number => number * number;


        public Action<int, Func<int, int>> DisplayResult = delegate (int result, Func<int, int> function)
        {
            Console.WriteLine(function(result));
        };

        static void Main(string[] args)
        {

        }
    }
}

1 Answer

Steven Parker
Steven Parker
171,245 Points

:point_right: Use parentheses around the argument list when there is more than one.

That's really the only difference from a lambda with one argument. Otherwise, the steps to convert from a single-statement delegate are the same:

  • remove the word "delegate"
  • remove the types from the arguments
  • remove the parentheses if there is only one argument :point_left: leave them this time
  • change the open brace to the lambda symbol (=>)
  • remove the closing brace

So, doing that to DisplayResult would look like this:

public Action<int, Func<int, int>> DisplayResult = (result, function) => Console.WriteLine(function(result));