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

Shelley Hicklin
Shelley Hicklin
22,460 Points

Trouble refactoring

Help! What am I missing. I keep getting " Bummer! Did you use the same variable names in the parameters of the lambda expression?"

Program.cs
using System;

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

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

        static void Main(string[] args)
        {

        }
    }
}

2 Answers

Steven Parker
Steven Parker
159,270 Points

Argument types are inferred in lambda expressions.

Also, a code block would not normally be used in a refactored single-line function. For example, let's look at the refactoring done for task 1. You wrote:

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

While the challenge accepted this, it is not a typical lambda refactoring for a single-line function. A more typical conversion would look like this:

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

Notice that the argument type is no longer needed, and the return expression can just be written by itself.

Now try using that example to refactor the DisplayResult function, and I'll bet you'll pass the challenge.