Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Need correct answer


Course


Querying With LINQ 253-minute C# Course


Actions and Func Objectives


Challenge Task 1 of 3


In the Program class, declare a public Func field named Square that takes an int and returns an int. Use an anonymous method to assign a delegate that takes an int parameter named number and returns the result of number * number.

My attempt #1:


using System;

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

When checking work:

Bummer! Did you declare a Func that takes an 'int' and returns an 'int'?


My attempt #2:


using System;

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

Bummer! Did you declare a 'public Func'?


Program.cs
using System;

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

2 Answers

Dave Harker
PLUS
Dave Harker
Courses Plus Student 15,510 Points

Hi Rajesh Thakur,

I think your first attempt was the closest :)
The challenge engine it seems is really fussy on this one ... I used your code ... and the check work return information

Bummer! Did you declare a 'public Func'?

// make it public and for syntactic craziness separate your argument and return type
// i.e. public Func<int, int> 
Func<int,int> Square = delegate(int number) 
    {
        return number * number;
    };

Nice work (crazy checking requirement aside) :dizzy:
Dave