What are Delegates in C# with simple example
In the previous artical I have discussed about IEnumerable and IEnumerator in C# , Useful shortcut keys of Visual studio.Today i am going to tell you about Delegates in C#.
Declaration -
Below is the basic example to understand Delegate -
Advantages of Delegates-
Output :
Keypoint to remember -
If a multicast delegate has some return type than it will return the result of the last method that is referenced by the delegate.
I hope you have liked Delegates in C# tutorial. I would like to have feedback from you. Your feedback, question ,queries or comments are always welcome.
Thanks.
Delegate is a type which holds the method(s) reference in an object. It is also referred to as a type safe function pointer.Whenever user need to create a delegate he/she need to use delegate keyword and delegate method's signature should match exactly with the methods which we are going to hold like same return types and same parameters otherwise delegate functionality won’t work if signature not match with methods.
Declaration -
Below is the basic example to understand Delegate -
Advantages of Delegates-
- Encapsulating the method's call from caller
- Effective use of delegate improves the performance of application
- Used to call a method asynchronously
Points to Remember -
- We can use delegates without parameters or with parameter list
- We should follow the same syntax as in the method (If we are referring to the method with two int parameters and int return type, the delegate which you are declaring should be in the same format. This is why it is referred to as type safe function pointer.)
Properties of Delegate -
- Delegates are similar to C++ function pointers, but are type safe.
- Delegates allow methods to be passed as parameters.
- Delegates can be used to define callback methods.
- Delegates can be chained together; for example, multiple methods can be called on a single event.
- Methods don't need to match the delegate signature exactly.
Multicast Delegate
It is a delegate which holds the reference of more than one method.Multicast delegates must contain only methods that return void, else there is a run-time exception.
Simple program that demonstrate multicast delegate -
Sum =9
Mul = 18
Keypoint to remember -
If a multicast delegate has some return type than it will return the result of the last method that is referenced by the delegate.
Below Example will clear the the above key point -
Output - 18
If you look carefully to above example,than you will see that last referenced method by delegate is function Mul(int a,int b) that is why output is 18.
The Delegate is added using the += operator and removed using the -= operator.
See the below example -
Output: 9 (because Mul method is deferenced from delegate so it will return the result of sum method that is last method that is referenced by delegate.
Output - 18
If you look carefully to above example,than you will see that last referenced method by delegate is function Mul(int a,int b) that is why output is 18.
The Delegate is added using the += operator and removed using the -= operator.
See the below example -
Output: 9 (because Mul method is deferenced from delegate so it will return the result of sum method that is last method that is referenced by delegate.
I hope you have liked Delegates in C# tutorial. I would like to have feedback from you. Your feedback, question ,queries or comments are always welcome.
Thanks.
What are Delegates in C# with simple example
Reviewed by CodiBucket
on
09:51
Rating:
No comments: