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

Completely lost help please reviewed video still lost

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.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    public class Program
    {
        public Action<int, Func<int, int>> DisplayResult = delegate (result, Func<int, int> operation) 
       {
       };


    }
}

3 Answers

Steven Parker
Steven Parker
172,267 Points

It looks like you're working on a completely different challenge here!

Try taking the instructions in small pieces:

  • "declare a public Func field ... " — but you are declaring an Action here.
  • "... named Square ... " — but you're declaring something named "DisplayResult".
  • "... that takes an int and returns an int." — your Action takes an int and returns a Func.
  • "... assign a delegate that takes an int parameter ... " — yours is taking two, one untyped and one Func.
  • "... named number ... " — your two paramerters are named "result" and "operation"
  • "... and returns the result of number * number." — you didn't get to the function body yet

I'm not sure where you got the names and specs you were using, but try again and read the instructions carefully. Break them down into pieces and implement them one piece at a time.

Benvienido Go
Benvienido Go
2,074 Points

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) { int number_square = operation(result); Console.WriteLine(number_square); };

Benvienido Go
Benvienido Go
2,074 Points

class Program { public static Func<int, int> Square = delegate (int number) { return number * number; }; public static Action<int, Func<int, int>> DisplayResult = delegate (int number, Func<int, int> square) { int number_square = Square(number); Console.WriteLine(string.Format("Square, {0}", number_square)); }; static void Main(string[] args) {
DisplayResult(6, Square); Console.ReadLine(); } }