1

How can we use delegates for abstraction? We can also use interfaces for abstraction so which is better? Is it with using interfaces or using delegates.

A simple practical example will help.

Thankyou.

Codor
  • 17,447
  • 9
  • 29
  • 56
srikar kulkarni
  • 630
  • 1
  • 6
  • 16
  • 1
    I think delegates are more about separation of concern than abstraction. – Davide Vitali Mar 12 '19 at 06:50
  • Wikipedia says "Functional programming languages commonly exhibit abstractions related to functions, such as lambda abstractions (making a term into a function of some variable) and higher-order functions (parameters are functions)." – Enigmativity Mar 12 '19 at 06:57
  • 1
    Really very broad and opinion based... Since delegates are really single method interfaces (like https://stackoverflow.com/questions/44912/java-delegates) it is very hard to find how one is "better" than another. – Alexei Levenkov Mar 12 '19 at 07:00

2 Answers2

0

1)Prevent extra function creation enter image description here

2)Events to prevent hard code enter image description here

Petr Tripolsky
  • 1,419
  • 15
  • 23
0

To answer the question in part, please consider the generic Linq extension method OrderBy, which is documented here. It is possible to use a delegate to provide a mapping from the sequence members to a type which is then used for the comparison proper. Hence, the actual sorting is decoupled from the ordering which is implicitly given via the implementation of the result type. The used delagate type permits a more abstract implementation of the sorting algorithm.

Codor
  • 17,447
  • 9
  • 29
  • 56
  • 1
    The funny thing you could do exactly the same with interfaces... not really sure how this example shows that one is better than another. – Alexei Levenkov Mar 12 '19 at 07:02
  • @AlexeiLevenkov Absolutely; however I believe that providing the same abstraction with an interface would result in a more lengthy implementation of the mapping. – Codor Mar 12 '19 at 07:04