dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Miss some concepts in understaning delegate ?

Hybrid View

  1. #1
    Join Date
    Oct 2006
    Posts
    123

    Miss some concepts in understaning delegate ?

    I understand the benefit of the delegate but i am not find example to convinced me for the usage.

    what i understnd is "you need your program to call a method, but you can't know what that method will be until run-time".

    when i see all example i am find that i doesn't need for delegate like this example
    Code:
    namespace MyFirstDelegate
    
    {
    
        //This delegate can point to any method,
    
        //taking two integers and returning an
    
        //integer.
    
        public delegate int MyDelegate(int x, int y);
    
        //This class contains methods that MyDelegate will point to.
    
        public class MyClass
    
        {
    
            public static int Add(int x, int y)
    
            {
    
                return x + y;
    
            }
    
            public static int Multiply(int x, int y)
    
            {
    
                return x * y;
    
            }
    
        }
    
        class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                //Create an Instance of MyDelegate
    
                //that points to MyClass.Add().
    
                MyDelegate del1 = new MyDelegate(MyClass.Add);
    
                //Invoke Add() method using the delegate.
    
                int addResult = del1(5, 5);
    
                Console.WriteLine("5 + 5 = {0}\n", addResult);
    
                //Create an Instance of MyDelegate
    
                //that points to MyClass.Multiply().
    
                MyDelegate del2 = new MyDelegate(MyClass.Multiply);
    
                //Invoke Multiply() method using the delegate.
    
                int multiplyResult = del2(5, 5);
    
                Console.WriteLine("5 X 5 = {0}", multiplyResult);
    
                Console.ReadLine();
    
            }
    
        }
    }
    because when I take object from the class I will determine what method need to be invoked or i can make condition.

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: Miss some concepts in understaning delegate ?

    Yes, you can. This code is just demonstrating how a delegate works in the most simple manner possible. If you want a good usecase for a delegate look at any of the asynchronous methods in the .NET framework which take an AsyncCallback delegate. For example with sockets it allows you to tell the socket to inform you when it's sent/received the required data by calling your delegate. You couldn't do that with if statements.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Oct 2006
    Posts
    123

    Re: Miss some concepts in understaning delegate ?

    Can I find any Sample Code for convinced Example.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)