gpt4 book ai didi

c# - 可以向条件语句添加更多指令

转载 作者:太空宇宙 更新时间:2023-11-03 17:41:26 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Using the ternary operator for multiple operations

(5 个回答)


7年前关闭。




大家好,如果我有以下内容

if(check)
{
count++;
push();
}
else
{
count--;
pop();
}

我想知道是否可以在条件语句中转换它

像这样的东西:
(check)?count++,push():count--,pop();

最佳答案

三元运算符需要返回值,因此将您的方法包装在委托(delegate)中并返回一些虚拟值(在本例中为 true )。你完成了..

int count = 0;
Func<bool> func1 = () =>
{
count++;
push();
return true;//To make compiler happy
};
Func<bool> func2 = () =>
{
count++;
push();
return true;//To make compiler happy
};

var dummy = (check) ? func1() : func2();

关于c# - 可以向条件语句添加更多指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20213067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com