oopdesign-patternsdelegates

Where do I use delegates?


What are some real world places that call for delegates? I'm curious what situations or patterns are present where this method is the best solution. No code required.


Solution

  • A delegate is a named type that defines a particular kind of method. Just as a class definition lays out all the members for the given kind of object it defines, the delegate lays out the method signature for the kind of method it defines.

    Based on this statement, a delegate is a function pointer and it defines what that function looks like.

    A great example for a real world application of a delegate is the Predicate. In the example from the link, you will notice that Array.Find takes the array to search and then a predicate to handle the criteria of what to find. In this case it passes a method ProductGT10 which matches the Predicate signature.