Skip to main content
added 180 characters in body
Source Link
unholysampler
  • 7.9k
  • 1
  • 32
  • 38

Delegates act as a way to parametrize some kind of action. The function that is being passed a delegate doesn't care how the action is done, just that it is performed.

For example, look at the IComparer interface. It allows you to sort a list without relying on how the type in the list is "naturally" sorted. However, when you look at the interface, it only defines one method. It seams rather wasteful to define a whole class that only implements a single function.

Delegates solve this problem1. You won't need to define a class, just the method. It allows you to remove boilerplate code and focus on what you want to do. Lambdas take this a step forward so that the function definition can be in-lined instead of defined somewhere else in the class.

1 If you look at Sort(), an overloaded version exists that uses a delegate.

Delegates act as a way to parametrize some kind of action. The function that is being passed a delegate doesn't care how the action is done, just that it is performed.

For example, look at the IComparer interface. It allows you to sort a list without relying on how the type in the list is "naturally" sorted. However, when you look at the interface, it only defines one method. It seams rather wasteful to define a whole class that only implements a single function.

Delegates solve this problem. You won't need to define a class, just the method. It allows you to remove boilerplate code and focus on what you want to do. Lambdas take this a step forward so that the function definition can be in-lined instead of defined somewhere else in the class.

Delegates act as a way to parametrize some kind of action. The function that is being passed a delegate doesn't care how the action is done, just that it is performed.

For example, look at the IComparer interface. It allows you to sort a list without relying on how the type in the list is "naturally" sorted. However, when you look at the interface, it only defines one method. It seams rather wasteful to define a whole class that only implements a single function.

Delegates solve this problem1. You won't need to define a class, just the method. It allows you to remove boilerplate code and focus on what you want to do. Lambdas take this a step forward so that the function definition can be in-lined instead of defined somewhere else in the class.

1 If you look at Sort(), an overloaded version exists that uses a delegate.

Source Link
unholysampler
  • 7.9k
  • 1
  • 32
  • 38

Delegates act as a way to parametrize some kind of action. The function that is being passed a delegate doesn't care how the action is done, just that it is performed.

For example, look at the IComparer interface. It allows you to sort a list without relying on how the type in the list is "naturally" sorted. However, when you look at the interface, it only defines one method. It seams rather wasteful to define a whole class that only implements a single function.

Delegates solve this problem. You won't need to define a class, just the method. It allows you to remove boilerplate code and focus on what you want to do. Lambdas take this a step forward so that the function definition can be in-lined instead of defined somewhere else in the class.