C# Querying With LINQ Functional Programming in C# Actions and Funcs

I don't understand the last part of this challenge

Can someone help me understand the last part of this challenge? I don't know what Console.WriteLine is supposed to print because I don't know what is going on in this example, really.

Task: "Inside the DisplayResult delegate, use Console.WriteLine to write the print the result of calling operation with the value of result."

using System;

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

        };

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

2 Answers

Steven Parker
Steven Parker
170,303 Points

:point_right: Calling is the same thing as invoking.

When you call a function, you name it with parentheses behind, like this: "function_name()". If you are calling it with the value of a parameter, then the parameter name goes inside the parentheses, like this: "function_name(parameter_name)".

So, in this case, calling operation with the value of result would be: "operation(result)".

Then using Console.WriteLine to print the result of doing all that would look like this:

            Console.WriteLine(operation(result));

Thank you. I think the wording of the task threw me off more than anything (result of result).

Stephen Wall
PRO
Stephen Wall
Pro Student 20,577 Points

Should it even be called result? It seems like you are passing the number to square and the function to square it.

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

Not sure if I am wrong on this but naming it result makes it seem like you are getting the result of the operation before you've even done the operation? Maybe I am missing something.