我想使用三元运算符来确定变量是否应该更改。
代码如下:
var c = "hello";
var appendWorld = false;
c = appendWorld ? string.Concat(c, " world") : c;
Run Code Online (Sandbox Code Playgroud)
由于 case appendWorld为false的值与之前相同,我想知道是否有更简洁的方法来编写此代码。
请记住,这只是一个简化的示例。
我希望能够编写这样的内容:如果appendWorld为falsec = appendWorld ? string.Concat(c, " world"); ,则c自动保持不变。
这样的事情存在吗?
我想使用LINQ检查myList!= null,然后执行Where。
我知道我会写
if (myList != null { myList.Where(p => p.x == 2 && p.y == 3); }
但我想使用: myList?.Where(p => p.x == 2 && p.y == 3);
这样行吗?
如果没有,您还知道其他哪些优雅的解决方案?