Domanda

My understanding is a delegate just point to a method.

If delegate A point to method B and method C through multiple casting, why can't we have delegate A pointing to method B and method B calling method C?

È stato utile?

Soluzione

If delegate A point to method B and method C through multiple casting, why can't we have delegate A pointing to method B and method B calling method C?

Sure, you can do that. But the entire point of delegates is that the delegate consumer doesn't need to know what the implementation is. And in delegates with multiple targets, the key point is that they don't need to know about each-other. Since events are implemented by delegates, it is common for an event to have multiple subscribers, which is perhaps the main usage of delegates with multiple targets.

However, in .NET all delegates support multiple targets, so it is rather moot.

As an aside; in your A/B/C scenario, you could also do:

SomeDelegateType instance = delegate {
    B();
    C();
};

which is a single delegate that calls B and C. Or you can do:

SomeDelegateType instance = new SomeDelegateType(B) + new SomeDelegateType(C);

or whatever you want, really. It doesn't matter hugely.

Altri suggerimenti

delegate void Del(string s);

class TestClass
{
static void Hello(string s)
{
    System.Console.WriteLine("  Hello, {0}!", s);
}

static void Goodbye(string s)
{
    System.Console.WriteLine("  Goodbye, {0}!", s);
}

static void Main()
{
    Del a, b, c, d;

    // Create the delegate object a that references 
    // the method Hello:
    a = Hello;

    // Create the delegate object b that references 
    // the method Goodbye:
    b = Goodbye;

    // The two delegates, a and b, are composed to form c: 
    c = a + b;

    // Remove a from the composed delegate, leaving d, 
    // which calls only the method Goodbye:
    d = c - a;

    System.Console.WriteLine("Invoking delegate a:");
    a("A");
    System.Console.WriteLine("Invoking delegate b:");
    b("B");
    System.Console.WriteLine("Invoking delegate c:");
    c("C");
    System.Console.WriteLine("Invoking delegate d:");
    d("D");
}
}
 /* Output:
Invoking delegate a:
Hello, A!
Invoking delegate b:
Goodbye, B!
Invoking delegate c:
Hello, C!
Goodbye, C!
Invoking delegate d:
Goodbye, D!
 */

When you define Multicast Delegate, it means that you can add as many as you need functions to this delegate. and invoking this multicast delegate will call all functions that are pointed by. Check this sample from MSDN:

using System;

// Define a custom delegate that has a string parameter and returns void. 
delegate void CustomDel(string s);

class TestClass
{
    // Define two methods that have the same signature as CustomDel. 
    static void Hello(string s)
    {
        System.Console.WriteLine("  Hello, {0}!", s);
    }

    static void Goodbye(string s)
    {
        System.Console.WriteLine("  Goodbye, {0}!", s);
    }

    static void Main()
    {
        // Declare instances of the custom delegate.
        CustomDel hiDel, byeDel, multiDel, multiMinusHiDel;

        // In this example, you can omit the custom delegate if you  
        // want to and use Action<string> instead. 
        //Action<string> hiDel, byeDel, multiDel, multiMinusHiDel; 

        // Create the delegate object hiDel that references the 
        // method Hello.
        hiDel = Hello;

        // Create the delegate object byeDel that references the 
        // method Goodbye.
        byeDel = Goodbye;

        // The two delegates, hiDel and byeDel, are combined to  
        // form multiDel. 
        multiDel = hiDel + byeDel;

        // Remove hiDel from the multicast delegate, leaving byeDel, 
        // which calls only the method Goodbye.
        multiMinusHiDel = multiDel - hiDel;

        Console.WriteLine("Invoking delegate hiDel:");
        hiDel("A");
        Console.WriteLine("Invoking delegate byeDel:");
        byeDel("B");
        Console.WriteLine("Invoking delegate multiDel:");
        multiDel("C");
        Console.WriteLine("Invoking delegate multiMinusHiDel:");
        multiMinusHiDel("D");
    }
}

/* Output:
Invoking delegate hiDel:
  Hello, A!
Invoking delegate byeDel:
  Goodbye, B!
Invoking delegate multiDel:
  Hello, C!
  Goodbye, C!
Invoking delegate multiMinusHiDel:
  Goodbye, D!
*/`

Hope it helps.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top