locked
When to use Delegate? RRS feed

  • Question

  • User-775847542 posted

    hello,

    I am very wondering about when to use delegate...I have google but I didnt get appropriate solution for it

    can any one tell me at which scenario I can use delegate.

    If possible than pls give example.

    Thanks in advance

    Saturday, August 24, 2013 3:44 AM

Answers

  • User2103319870 posted

    Hi,

    A simple example for delegate is given below

    // Declaration
        public delegate void SimpleDelegate();
    
        class TestDelegate
        {
            public static void MyFunc()
            {
                Console.WriteLine("I was called by delegate ...");
            }
    
            public static void Main()
            {
                // Instantiation
                SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);
    
                // Invocation
                simpleDelegate();
            }
        }

    Source URL

    Below links will help you understand Delegate in detail

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 24, 2013 11:30 AM
  • User2103319870 posted

    mauleshmevada

    hi,

    can you tell me what is diffrance of your code and below code?

    class TestDelegate
    {
    public static void MyFunc()
    {
    Console.WriteLine("I was called by delegate ...");
    }

    public static void Main()
    {
    MyFunc();
    }
    }

    thanks,

    Hi,

    Aplogizing for the late reply. You are calling your method like normal method call in c#. You dont have the advantages which delegates offer if you call your function like this.To know about the advantages of using delegates refer the below link</div> <div>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 30, 2013 9:19 AM

All replies

  • User-1360095595 posted

    Biggest use of delegates, that I'm aware of, is for raising event. A delegate is just a pointer to a method that will be called one way or another. In an event, when it's time to raise the actual event, any delegates that are registered will be called. 

    As for examples, Google/Bing cam provide you a ton of examples.

    Saturday, August 24, 2013 5:28 AM
  • User-775847542 posted

    hi, can you give example how rise event using delegate..

    Thanks

    Saturday, August 24, 2013 6:48 AM
  • User2103319870 posted

    Hi,

    A simple example for delegate is given below

    // Declaration
        public delegate void SimpleDelegate();
    
        class TestDelegate
        {
            public static void MyFunc()
            {
                Console.WriteLine("I was called by delegate ...");
            }
    
            public static void Main()
            {
                // Instantiation
                SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);
    
                // Invocation
                simpleDelegate();
            }
        }

    Source URL

    Below links will help you understand Delegate in detail

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 24, 2013 11:30 AM
  • User-775847542 posted

    hi,

    can you tell me what is diffrance of your code and below code?

    class TestDelegate
    {
    public static void MyFunc()
    {
    Console.WriteLine("I was called by delegate ...");
    }

    public static void Main()
    {
    MyFunc();
    }
    }

    thanks,

    Monday, August 26, 2013 2:44 AM
  • User488649576 posted

    Hi,

    Please refer to the link below:

    http://social.msdn.microsoft.com/Forums/en-US/f629c34d-6523-433a-90b3-bb5d445c5587/differences-between-delegate-invoke-and-call-a-method-directly\

    It tells us the difference between the two calling.

     

    Friday, August 30, 2013 6:36 AM
  • User2103319870 posted

    mauleshmevada

    hi,

    can you tell me what is diffrance of your code and below code?

    class TestDelegate
    {
    public static void MyFunc()
    {
    Console.WriteLine("I was called by delegate ...");
    }

    public static void Main()
    {
    MyFunc();
    }
    }

    thanks,

    Hi,

    Aplogizing for the late reply. You are calling your method like normal method call in c#. You dont have the advantages which delegates offer if you call your function like this.To know about the advantages of using delegates refer the below link</div> <div>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 30, 2013 9:19 AM