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# Lambda Expressions

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
229,644 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.

Thanks! that got it!