Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

luke hammer
luke hammer
25,513 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
231,198 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));