What is a delegate?
Delegate is type which holds the method(s) reference in an object. it is also referred as a type safe function pointers.
Advantages:
Ø Encapsulating the method's call from caller
Ø Effective use of Delegate improves the performance of application.
Ø Used to call a method asynchronously.
Syntax:
public delegate type_of_delegate delegate_name()
Example : public delegate int mydelegate(int param1,int param2)
Note
Ø you can use delegate without parameter or with parameter list
Ø you should follow the same syntax as in the method
If you are referring the method with two int parameters and int return type the delegate which you are declaring should be the same format. This is how it is referred as type safe function pointer.
Features of delegates:
Ø A delegate represents a class.
Ø A delegate is type-safe.
Ø We can use delegates both for static and instance methods
Ø We can combine multiple delegates into a single delegate.
Ø Delegates are often used in event-based programming, such as publish/subscribe.
Ø We can use delegates in asynchronous-style programming.
Ø We can define delegates inside or outside of classes.
No comments:
Post a Comment