c# - 多播委托(delegate)不明确

标签 c# multicastdelegate

我有以下代码

    StringOperations sumString, reverseString, lowerString, upperString, multicastString;

    sumString = new StringOperations(sum);
    reverseString = new StringOperations(reverse);
    lowerString = new StringOperations(lower);
    upperString = new StringOperations(upper);

    multicastString = upperString + lowerString + reverseString + sumString;

    int count = 4;

    if (!checkBox1.Checked)
    {
            multicastString -= upperString;
            count--;
    }
    if (!checkBox2.Checked)
    {
            multicastString -= reverseString;
            count--;
    }
    if (!checkBox3.Checked)
    {
            multicastString -= lowerString;
            count--;
    }
    if (!checkBox4.Checked)
    {
            multicastString -= sumString;
            count--;
    }
    if (count > 0)
    {
            string test = multicastString(textBox1.Text);
    }

选择大写和小写复选框时,它只显示我的小写函数的结果。

如果我选择大写、小写和反向复选框,那么它只会显示反向功能的结果。

我的代表在下面

delegate string StringOperations(string str);

我正在使用多播委托(delegate)并返回 string,如上面的代码所示。请让我知道我做错了什么?

最佳答案

当您有一个附加了多个处理程序的委托(delegate)时,您仍然只会获得一个返回值。没有直接的方法来获取其他值,自然您不能以将一个返回值发送到另一个的方式链接处理函数。您唯一会得到的是返回最后附加的处理程序的返回值。

这里真的没有模棱两可的行为,这就是它的工作方式。如果你想链接函数,你必须使用不同的方法然后是委托(delegate)。在此示例中,您只需调用函数即可。

关于c# - 多播委托(delegate)不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32027944/

相关文章:

c# - 如何在 C# 中处理多播委托(delegate)中的异常?

c# - 如何在 C# 中处理多播委托(delegate)中的异常?

c# - 如何以编程方式检查安装了哪个版本的 WMI

c# - 如何在C#(ASP.Net)中调用参数化存储过程?

c# - 使用属性对两个复杂类型的泛型集合进行排序

c# - 程序网格减慢统一场景

c# - 使用 MulticastDelegate 作为参数同时避免 DynamicInvoke

c# - 连接到我的数据库时出错(MySQL、C#)

ios - Swift 语言多播委托(delegate)