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

Rajesh Thakur
Rajesh Thakur
1,661 Points

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;
        };
    }
}

1 Answer

Dave Harker
PRO
Dave Harker
Pro Student 13,992 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