我有以下代码
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/