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));
    };

7 Answers

Steven Parker
Steven Parker
173,991 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) =>

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

Dávid Polyovka
Dávid Polyovka
4,229 Points

The solution is:

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