c# - Can I change the use of two ? : shortcuts to use the new switch expression syntax?
问题描述
I have code that looks like this. I wonder if I could replace this with a switch expression type of code:
var ptsMsg1 = !App.cards ?
"" :
selectedPoints == pointsCount ?
" This is the same number as when you started your practice." :
$" This is {(selectedPoints - pointsCount)} more.";
解决方案
C# 8 switch 表达式可用于:
var msg2 = (App.cards, samePoints: selectedPoints == pointsCount) switch
{
{ cards: false } => "",
{ samePoints: true } => " This is the same number as when you started your practice.",
_ => $" This is {(selectedPoints - pointsCount)} more.",
};
我的回答只是对问题的回应。无论如何,这个或 OP 的代码是否更易于维护是主观的。
推荐阅读
- html - 适合内容的背景颜色
标签
- c# - C# Visual Studio IDE
- android - 从android中的webview获取点击事件
- java - Java Scripting API (JSR223) groovy 语言限制
- python - 数据类:NameError:未定义名称“WORD_TYPE”
- javascript - GTM 层不推送数据
- java - java.lang.IllegalArgumentException:警告此类型名称不匹配:queryKey [Xlint:invalidAbsoluteTypeName]
- javascript - Array.includes 总是返回 false
- java - 如何在数组列表的侧面对象中的对象中查找重复的 json 元素?
- concurrency - 为什么 erlang spawn 函数调用中出现语法错误 - “之前的语法错误:')'”?