[英]Multicast Delegates
我們可以使用泛型使用多播委托嗎? 請使用以下代碼說明如何實現。
delegate string multidelegate<T1,T2>(T1 a,T2 b);
class mylogic
{
public void Method1(int a, int b)
{
Console.WriteLine("This is Method1 where value of multiplication is {0}",a*b);
}
public void Method2(double a, double b)
{
Console.WriteLine("This is Method2 where the value of multiplication is {0}",a*b);
}
}
class Program
{
static void Main(string[] args)
{
multidelegate<int,int> del = new multidelegate<int,int>(new mylogic().Method1).Tostring();
del += Convert.ToString(new multidelegate<double,double>(new mylogic().Method2).Tostring());
del(32,51);
}
}
C#中的所有委托都是多播委托,並且您可以具有通用委托,因此可以,您可以具有通用多播委托。 所有通用委托都是通用多播委托。
但是,如果通用委托的兩個實例具有不同的通用參數,則不能合並它們。 您只能將具有相同泛型類型參數的同一委托的實例組合在一起。 這是有道理的,因為能夠合並代表的重點是他們需要具有相同的合同; 他們需要接受相同的參數並輸出相同類型的輸出。 如果通用參數不同,則不是這種情況。
我們可以使用泛型使用多播委托嗎? 是是多播委托是通過您的委托呼叫多個訂戶並將呼叫結果返回到最后一個訂戶的操作。
您應該使用Func委托而不是多委托
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.