Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 36 - C# Tutorial - Delegates

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);
    }
}

Part 36 - C# Tutorial - Delegates


16 comments:

  1. Very nice and clear !!!

    ReplyDelete
  2. Simple and concise, Thanks!

    ReplyDelete
  3. very nice work >> thanks

    ReplyDelete
  4. Very well explained...thanks

    ReplyDelete
  5. You are gift to humanity. I will be your fan for life. Can you make videos on MVC?

    ReplyDelete
  6. thank 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.

    ReplyDelete
  7. Hi 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.

    ReplyDelete
  8. Great 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.

    ReplyDelete
  9. Never 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.

    ReplyDelete
  10. Hi Venkat
    in 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

    ReplyDelete
    Replies
    1. Are delegates created by .Net Framework and can be use.

      Delete
  11. Good Explanation.

    Why use delegate?

    ReplyDelete
    Replies
    1. 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.

      Delete
  12. can we create delegate Generic type like this
    public delegate List results(string HotelName, string HotelAdderess, string RoomType, int HotelPhoneNumber);

    ReplyDelete
  13. its clear and so easy thankyou for this videos

    ReplyDelete
  14. Thanks Venkat for explaining Delegates in Simple, clear and easily understandable language. I understand Delegates after 5yrs in my career.
    Thanks again.

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.