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

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