C# Querying With LINQ Functional Programming in C# Delegates

help me

Challenge Task 2 of 5

Create a public static method named Add that matches the signature of the MathOperation delegate. Inside the method, return the result of number + number. Bummer! Your code could not be compiled. Please click on "Preview" to view the compiler errors. Restart Preview Get Help Recheck work Program.cs

1 using System; 2 ‚Äč 3 namespace Treehouse.CodeChallenges 4 { 5 class Program 6 { 7

8 delegate int MathOperation(int number); 9 public static void Add(string MathOperation); 10 { 11 console.WriteLine(number + number); 12 } 13 static void Main(string[] args) 14 { 15

16 } 17 } 18 }

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {

        delegate int MathOperation(int number);
         public static void Add(string MathOperation);
        { 
            console.WriteLine(number + number);
        }
        static void Main(string[] args)
        {

        }
    }
}

1 Answer

Taylor Michel
Taylor Michel
15,632 Points

You should change the name of your parameter from MathOperation to number and you need to change your return type of your method to int instead of void.

By making your new method return the same type and accept the same type of parameters as the delegate, you will then be able to assign that method to the delegate.

Your final code should look something like this.

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        delegate int MathOperation(int number);
        public static int Add(int number)
        {
            return number + number;
        }

        static void Main(string[] args)
        {

        }
    }
}

https://msdn.microsoft.com/en-us/library/ms173171.aspx

^^^ A good read on Delegates ^^^

Hope this helps!

Happy coding!

thank you so much taylor