为什么此多播委托不适用于 C# 中的函数?
Why this multicast delegation isn’t working with functions in C#?
我是 C# 的新手,正在练习多播委托,我写了这段代码:
class MyClass
{
public static double Calculate(int x, double z)
{
double Result = (x / z);
return Result;
}
public static double Calculate2(int x, double z)
{
double Result = (x * z);
return Result;
}
}
public class Program
{
public delegate double MyDlgt(int Number1, double Number2);
public static void Main()
{
Console.WriteLine("What is You First Value ? ");
int NUM1 = int.Parse(Console.ReadLine());
Console.WriteLine("What is You Second Value ? ");
Double NUM2 = Double.Parse(Console.ReadLine());
MyDlgt FirstDlgt = new MyDlgt(MyClass.Calculate);
MyDlgt SecondDlgt = new MyDlgt(MyClass.Calculate2);
Console.WriteLine("<---------Simple Delegate --------->");
Console.WriteLine("The Division Result is : " + FirstDlgt(NUM1, NUM2));
Console.WriteLine("The Multiply Result is : " + SecondDlgt(NUM1, NUM2));
Console.WriteLine("");
Console.WriteLine("<---------MultiCast Delegate --------->");
MyDlgt MultiCast = FirstDlgt + SecondDlgt;
Console.WriteLine("The Result is : " + MultiCast(NUM1, NUM2));
Console.Read();
}
}
多播只显示第二个功能我不知道为什么。
结果是
What is You First Value ?
10
What is You Second Value ?
20
<---------Simple Delegate --------->
The Division Result is : 0.5
The Multiply Result is : 200
<---------MultiCast Delegate --------->
The Result is : 200
我尝试使用 void 进行多播并且工作正常
class MyClass
{
public static void Calculate(int x, double z)
{
Console.WriteLine("The Result is :"+ (x / z));
}
public static void Calculate2(int x, double z)
{
Console.WriteLine("The Result is :" + (x * z));
}
public static void Main()
{
Console.WriteLine("What is You First Value ? ");
int NUM1 = int.Parse(Console.ReadLine());
Console.WriteLine("What is You Second Value ? ");
Double NUM2 = Double.Parse(Console.ReadLine());
MyDlgt FirstDlgt = new MyDlgt(MyClass.Calculate);
MyDlgt SecondDlgt = new MyDlgt(MyClass.Calculate2);
Console.WriteLine("<---------Simple Delegate --------->");
FirstDlgt.Invoke(NUM1, NUM2);
SecondDlgt.Invoke(NUM1, NUM2);
Console.WriteLine("");
Console.WriteLine("<---------MultiCast Delegate --------->");
MyDlgt MultiCast = FirstDlgt + SecondDlgt;
MultiCast(NUM1, NUM2);
Console.ReadLine();
}
}
}
结果正确:
What is You First Value ?
10
What is You Second Value ?
20
<---------Simple Delegate --------->
The Result is :0.5
The Result is :200
<---------MultiCast Delegate --------->
The Result is :0.5
The Result is :200
为什么它使用 void 而不是使用 return 的函数?
抱歉这么久 post,提前致谢
根据 spec:
If the delegate invocation includes output parameters or a return value, their final value will come from the invocation of the last delegate in the list.
如果您需要对单个结果进行更多控制,请使用 GetInvocationList()
:
foreach (MyDlgt myDlgt in MultiCast.GetInvocationList())
{
double result = myDlgt(NUM1, NUM2);
}
我是 C# 的新手,正在练习多播委托,我写了这段代码:
class MyClass
{
public static double Calculate(int x, double z)
{
double Result = (x / z);
return Result;
}
public static double Calculate2(int x, double z)
{
double Result = (x * z);
return Result;
}
}
public class Program
{
public delegate double MyDlgt(int Number1, double Number2);
public static void Main()
{
Console.WriteLine("What is You First Value ? ");
int NUM1 = int.Parse(Console.ReadLine());
Console.WriteLine("What is You Second Value ? ");
Double NUM2 = Double.Parse(Console.ReadLine());
MyDlgt FirstDlgt = new MyDlgt(MyClass.Calculate);
MyDlgt SecondDlgt = new MyDlgt(MyClass.Calculate2);
Console.WriteLine("<---------Simple Delegate --------->");
Console.WriteLine("The Division Result is : " + FirstDlgt(NUM1, NUM2));
Console.WriteLine("The Multiply Result is : " + SecondDlgt(NUM1, NUM2));
Console.WriteLine("");
Console.WriteLine("<---------MultiCast Delegate --------->");
MyDlgt MultiCast = FirstDlgt + SecondDlgt;
Console.WriteLine("The Result is : " + MultiCast(NUM1, NUM2));
Console.Read();
}
}
多播只显示第二个功能我不知道为什么。
结果是
What is You First Value ?
10
What is You Second Value ?
20
<---------Simple Delegate --------->
The Division Result is : 0.5
The Multiply Result is : 200
<---------MultiCast Delegate --------->
The Result is : 200
我尝试使用 void 进行多播并且工作正常
class MyClass
{
public static void Calculate(int x, double z)
{
Console.WriteLine("The Result is :"+ (x / z));
}
public static void Calculate2(int x, double z)
{
Console.WriteLine("The Result is :" + (x * z));
}
public static void Main()
{
Console.WriteLine("What is You First Value ? ");
int NUM1 = int.Parse(Console.ReadLine());
Console.WriteLine("What is You Second Value ? ");
Double NUM2 = Double.Parse(Console.ReadLine());
MyDlgt FirstDlgt = new MyDlgt(MyClass.Calculate);
MyDlgt SecondDlgt = new MyDlgt(MyClass.Calculate2);
Console.WriteLine("<---------Simple Delegate --------->");
FirstDlgt.Invoke(NUM1, NUM2);
SecondDlgt.Invoke(NUM1, NUM2);
Console.WriteLine("");
Console.WriteLine("<---------MultiCast Delegate --------->");
MyDlgt MultiCast = FirstDlgt + SecondDlgt;
MultiCast(NUM1, NUM2);
Console.ReadLine();
}
}
}
结果正确:
What is You First Value ?
10
What is You Second Value ?
20
<---------Simple Delegate --------->
The Result is :0.5
The Result is :200
<---------MultiCast Delegate --------->
The Result is :0.5
The Result is :200
为什么它使用 void 而不是使用 return 的函数?
抱歉这么久 post,提前致谢
根据 spec:
If the delegate invocation includes output parameters or a return value, their final value will come from the invocation of the last delegate in the list.
如果您需要对单个结果进行更多控制,请使用 GetInvocationList()
:
foreach (MyDlgt myDlgt in MultiCast.GetInvocationList())
{
double result = myDlgt(NUM1, NUM2);
}