A delegate is a type safe function pointer.That is, they hold reference(Pointer) to a function.
The signature of the delegate must match the signature of the function, the delegate points to, otherwise you get a compiler error. This is the reason delegates are called as type safe function pointers.
A Delegate is similar to a class. You can create an instance of it, and when you do so, you pass in the function name as a parameter to the delegate constructor, and it is to this function the delegate will point to.
Tip to remember delegate syntax: Delegates syntax look very much similar to a method with a delegate keyword.
Sample Delegate Program:
using System;
// Delegate Declaration.
public delegate void HelloFunctionDelegate(string Message);
class Pragim
{
public static void Main()
{
// Create the instance of the delegate and pass in the function
// name as the parameter to the constructor. The passed in
// function signature must match the signature of the delegate
HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);
// Invoke the delegate, which will invoke the method
del("Hello from Delegte");
}
public static void Hello(string strMessge)
{
Console.WriteLine(strMessge);
}
}
The signature of the delegate must match the signature of the function, the delegate points to, otherwise you get a compiler error. This is the reason delegates are called as type safe function pointers.
A Delegate is similar to a class. You can create an instance of it, and when you do so, you pass in the function name as a parameter to the delegate constructor, and it is to this function the delegate will point to.
Tip to remember delegate syntax: Delegates syntax look very much similar to a method with a delegate keyword.
Sample Delegate Program:
using System;
// Delegate Declaration.
public delegate void HelloFunctionDelegate(string Message);
class Pragim
{
public static void Main()
{
// Create the instance of the delegate and pass in the function
// name as the parameter to the constructor. The passed in
// function signature must match the signature of the delegate
HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);
// Invoke the delegate, which will invoke the method
del("Hello from Delegte");
}
public static void Hello(string strMessge)
{
Console.WriteLine(strMessge);
}
}
Part 36 - C# Tutorial - Delegates
Very nice and clear !!!
ReplyDeleteSimple and concise, Thanks!
ReplyDeletevery nice work >> thanks
ReplyDeleteVery well explained...thanks
ReplyDeleteYou are gift to humanity. I will be your fan for life. Can you make videos on MVC?
ReplyDeletethank you for the videos sir. it would be more clear and easily understandable if you keep break point and debug so that we can understand workflows.
ReplyDeleteHi Prabhakar,I am saying heartfully,i didnt find this kind of tutorials sir(www.pragimtech.com). its really super.Please provide sharepoint videos also.i am looking for that.Thx in Advance.
ReplyDeleteGreat video and explanation, I read about delegates in the book, but could not get the idea. After watching your video everything is clear now. One more great video. Thanks.
ReplyDeleteNever understood delegates for the last 5 years except that it is function pointer. Now I got a clear understanding. Cannot restrain from giving you five thumbs up.
ReplyDeleteHi Venkat
ReplyDeletein part 98 of C#, you say that
Predicate employeePredicate = new Predicate(FindEmployee)
is a delegate:
my question is, if this is a delegate then why we don't have the name "Delegate" in it?
thanks for your help
Br
Ardevan
Are delegates created by .Net Framework and can be use.
DeleteGood Explanation.
ReplyDeleteWhy use delegate?
he already said he will explain that in a later time, all you need to know is how to use delegates so when he starts to work with it you will be ready.
Deletecan we create delegate Generic type like this
ReplyDeletepublic delegate List results(string HotelName, string HotelAdderess, string RoomType, int HotelPhoneNumber);
its clear and so easy thankyou for this videos
ReplyDeleteThanks Venkat for explaining Delegates in Simple, clear and easily understandable language. I understand Delegates after 5yrs in my career.
ReplyDeleteThanks again.
is it necessary that a delegates pointing to a function must be static,, if not then how can we pass a non static function to that delegate???
ReplyDelete