Delegate

-> A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke or call the method through the delegate instance.
-> Delegates are used to pass methods as arguments to other methods. Event handlers are nothing more than methods that are invoked through delegates.
-> Delegates can be used to define callback methods.
-> Delegates can be chained together; multiple methods can be called on a single event.

There are three types of delegates that can be used in C#.

  • Single Delegate
  • Multicast Delegate
  • Generic Delegate

Single Delegate
-> Single delegate can be used to invoke a single method.

Multicast Delegate
-> can be used to invoke multiple methods. The delegate instance can do multicasting (adding new method on existing delegate instance) using + operator and – operator can be used to remove a method from a delegate instance. All methods will invoke in sequence as they are assigned.

Generic Delegate
-> don’t require to define the delegate instance in order to invoke the methods. There are three types of generic delegates.
-> was introduced in .Net 3.5

1) Func
2) Action
3) Predicate

Action Delegate Type
-> The generic Action delegate represents a method that returns void. Different versions of Action take between 0 and 18 input parameters.
-> The action delegate that takes two arguments.

Func Delegate Type
-> Func in short is parameterized delegate.
-> The generic Func delegate represents a method that returns a value.
-> Func handles many arguments.
-> It provides a way to store anonymous methods in a generalized and simple way.

Leave a Reply

Your email address will not be published. Required fields are marked *