gpt4 book ai didi

c# - 如何调用所有事件处理程序订阅者并获得他们的结果?

转载 作者:行者123 更新时间:2023-11-30 19:25:22 25 4
gpt4 key购买 nike

我有这个片段:

public static void Main()
{
OnComparaison += LePlusPetit;
OnComparaison += LePlusGrand;
Console.WriteLine(OnComparaison(0, 9));
Console.ReadKey();
}

public static int LePlusPetit(object obj1, object obj2)
{
int int1 = (int)obj1;
int int2 = (int)obj2;
return (int1 < int2) ? int1 : int2;
}

public static int LePlusGrand(object obj1, object obj2)
{
int int1 = (int)obj1;
int int2 = (int)obj2;
return (int1 > int2) ? int1 : int2;
}

public delegate int Comparer(object obj1, object obj2);
public static event Comparer OnComparaison;

我的结果总是 9。所以我需要知道:

  1. 是否只执行最后订阅的事件处理程序而不是所有订阅者?
  2. 如果是这样,我如何修改代码,使所有订阅的事件处理程序在事件触发时执行?

最佳答案

Does only the last subscribed event handler is executed not all the subscribers?

不是,都执行了但是结果是最后执行的方法的返回值。

关于c# - 如何调用所有事件处理程序订阅者并获得他们的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28989901/

25 4 0
文章推荐: c# - 使用 onServerClick 获取 href 值
文章推荐: javascript - 使变量在整个 spec.js 文件中可用
文章推荐: c# - 任务并行库 - Task.Delay() 用法
文章推荐: c# - 我可以将 List 用作方法中的形式参数吗?