Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# Querying With LINQ Functional Programming in C# LINQ Method Syntax

Step 2. Now, refactor the DisplayResult delegate to use a lambda expression?

I'm not understanding this step.

Current Go:

    public Func<int, int> Square => delegate (int number)
    {
        return number * number;
    };

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

8 Answers

Steven Parker
Steven Parker
218,691 Points

The challenge "check work" function seems to be broken.

Not here, but in step one. It should not have passed your refactoring of Square.

A correctly refactored Square would look more like this:

        public Func<int, int> Square = number => number * number;

Notice that the equal sign (=) remains, but the term "delegate" does not. Also notice that the "goes to" symbols (=>) are placed between the argument and the returned value. Parentheses are not needed for a single argument, and braces are not needed for a return expression or a single statement.

Try using these guidelines for step 2, and/or review the video.

Also, you might want to report the challenge checker issue as a bug on the Support page.

For task two, remove all references to variables like ints, Func and the delegate too.

= (result, operation) =>

Dávid Polyovka
Dávid Polyovka
4,230 Points

The solution is:

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

Got it thanks!

I still cannot get the second task. Help please!

First task I did

public Func<int, int> Square = (number) => 

and passed.

Second task I tried

public Action<int, Func<int, int>> DisplayResult => (result, Func<int,int> function) =>

.. Nope.. Didn't work. And Considering the incredible short introduction to Lambda I dont think I can solve it.

Another question I have to bypass :/

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

this should help

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

Because I have a hell of a time myself with this, here's what worked for me.