c# - 从 if 语句 C# 执行 else 语句
问题描述
下面是我正在使用的 if-else 条件语句。我正在考虑删除 //Statements 2 中的代码重复
if (true)
{
List<string> usersList=getUsersList();
if(usersList.Length > 0){
//Statements 1
..............
}
else{
//Statements 2
.............
}
}
else
{
//Statements 2
.............
}
在上面的代码中需要重复语句 2。有没有办法将执行发送到主 else 循环。
解决方案
也许是这样的?
bool runSecondBlock = !firstCondition;
if (firstCondition)
{
List<string> usersList=getUsersList();
if(usersList.Length > 0){
//Statements 1
..............
}
else {
runSecondBlock = true;
}
}
if (runSecondBlock)
{
//Statements 2
.............
}
推荐阅读
- database - 文档协作数据库架构
- c - 我在 c 中的 printf 函数没有打印任何内容
- reactjs - 无法访问 echarts 中的 echarts 实例进行反应
- python - 将浮点数转换为字符串 numba python numpy 数组
- docker - 了解 docker 卷和卷选项
- json - 如何在反应js中访问json对象内的数组
- database - 如何使用 INIT=RUNSCRIPT FROM 'mytestscript.sql' 加载 sql 脚本文件并检查它是否已加载
- sequelize.js - 如何将 sequellize.fn 与具有超过 1 个参数的 SQL TIMESTAMPDIFF 函数一起使用
- c# - 如何在运行时替换 c# ToString() 方法
- android - 想要以编程方式从 ZEBRA DS2278 读取扫描的条码 Android