0

I'm pretty new to Delegates, Expression Lambda and local Methods. But can anyone explain the difference between these two code blocks?

public static void Main()
{
    bool IsDivisible(int x, int y)
    {
        return x % y == 0;
    }

    Func<int, int, bool> IsDivisibleFunc = (x, y) =>
    {
        return x % y == 0;
    };
}

Edit: Added a Main Method around it

MindSwipe
  • 7,193
  • 24
  • 47
  • Read here: https://stackoverflow.com/questions/10707000/delegate-methods-vs-general-methods – Bartosz Olchowik Oct 22 '18 at 12:00
  • One of examples to see a diff: Func you can pass as parameter to a method. – Renatas M. Oct 22 '18 at 12:02
  • @Reniuz: You can use a method group conversion to create a delegate from a local method, of course. – Jon Skeet Oct 22 '18 at 12:05
  • @JonSkeet yes, you can do that, but I was talking about particular OPs examples. :) – Renatas M. Oct 22 '18 at 12:14
  • @Reniuz so there is no difference other than being able to pass one as a parameter without having to use method group conversion? Edit: And other than being able to make the Func anonymous – MindSwipe Oct 22 '18 at 12:27
  • It was an example to understand where you can use one over another. Actually the difference is that one is a method and other is reference/pointer to a method. And you can extend your examples with non anonymous `Func ReferenceToIsDivisibleMethod = IsDivisible;` which will be a reference to your local method. – Renatas M. Oct 22 '18 at 12:45
  • Here is the answer to exactly your question: [Local function vs Lambda C# 7.0](https://stackoverflow.com/questions/40943117/local-function-vs-lambda-c-sharp-7-0) – AlbertK Oct 22 '18 at 12:50

0 Answers0