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# 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
229,732 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
PLUS
Stephen Wall
Courses Plus Student 27,294 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.