问题描述
powershell 中是否有空合并运算符?
我希望能够在 powershell 中执行这些 c# 命令:
var s = myval ??新价值";var x = myval == null ?"" : 其他值;
Powershell 7+
Powershell 7 在 Powershell 中引入了原生空值合并、空值条件赋值和三元运算符.
空合并
$null ??100 # 结果是 100评价"??(Expensive-Operation "Not Evaluated") # 这里的右边没有被评估
空条件赋值
$x = $null$x ??= 100 # $x 现在是 100$x ??= 200 # $x 仍然是 100
三元运算符
$true ?返回此值":未计算此表达式"$假?未计算此表达式":返回此值"
以前的版本:
不需要 Powershell 社区扩展,您可以使用标准的 Powershell if 语句作为表达式:
variable = if (condition) { expr1 } else { expr2 }
所以要替换您的第一个 C# 表达式:
var s = myval ??新价值";
成为以下之一(取决于偏好):
$s = if ($myval -eq $null) { "新值" } else { $myval }$s = if ($myval -ne $null) { $myval } else { "新值" }
或者取决于您可以使用的 $myval 可能包含的内容:
$s = if ($myval) { $myval } else { "新值" }
和第二个 C# 表达式以类似的方式映射:
var x = myval == null ?"" : 其他值;
变成
$x = if ($myval -eq $null) { "" } else { $otherval }
说句公道话,这些都不是很活泼,也远不如 C# 形式那么舒服.
您也可以考虑将其包装在一个非常简单的函数中以提高可读性:
function Coalesce($a, $b) { if ($a -ne $null) { $a } else { $b } }$s = Coalesce $myval 新值"
或者可能是,IfNull:
function IfNull($a, $b, $c) { if ($a -eq $null) { $b } else { $c } }$s = IfNull $myval "新值" $myval$x = IfNull $myval "" $otherval
如您所见,一个非常简单的函数可以为您提供相当多的语法自由.
更新:混合中需要考虑的一个额外选项是更通用的 IsTrue 函数:
function IfTrue($a, $b, $c) { if ($a) { $b } else { $c } }$x = IfTrue ($myval -eq $null) "" $otherval
然后结合 Powershell 声明看起来有点像运算符的别名的能力,你最终得到:
新别名??"合并$s = ??$myval "新值"新别名 "?:" IfTrue$ans = ?: ($q -eq "生命的意义") 42 $otherval
显然,这不会符合每个人的口味,但可能是您正在寻找的.p>
正如 Thomas 所指出的,C# 版本与上述版本之间的另一个细微差别是 C# 执行参数的短路,但涉及函数/别名的 Powershell 版本将始终评估所有参数.如果这是一个问题,请使用 if
表达式形式.
Is there a null coalescing operator in powershell?
I'd like to be able to do these c# commands in powershell:
var s = myval ?? "new value";
var x = myval == null ? "" : otherval;
Powershell 7+
Powershell 7 introduces native null coalescing, null conditional assignment, and ternary operators in Powershell.
Null Coalescing
$null ?? 100 # Result is 100
"Evaluated" ?? (Expensive-Operation "Not Evaluated") # Right side here is not evaluated
Null Conditional Assignment
$x = $null
$x ??= 100 # $x is now 100
$x ??= 200 # $x remains 100
Ternary Operator
$true ? "this value returned" : "this expression not evaluated"
$false ? "this expression not evaluated" : "this value returned"
Previous Versions:
No need for the Powershell Community Extensions, you can use the standard Powershell if statements as an expression:
variable = if (condition) { expr1 } else { expr2 }
So to the replacements for your first C# expression of:
var s = myval ?? "new value";
becomes one of the following (depending on preference):
$s = if ($myval -eq $null) { "new value" } else { $myval }
$s = if ($myval -ne $null) { $myval } else { "new value" }
or depending on what $myval might contain you could use:
$s = if ($myval) { $myval } else { "new value" }
and the second C# expression maps in a similar way:
var x = myval == null ? "" : otherval;
becomes
$x = if ($myval -eq $null) { "" } else { $otherval }
Now to be fair, these aren't very snappy, and nowhere near as comfortable to use as the C# forms.
You might also consider wrapping it in a very simple function to make things more readable:
function Coalesce($a, $b) { if ($a -ne $null) { $a } else { $b } }
$s = Coalesce $myval "new value"
or possibly as, IfNull:
function IfNull($a, $b, $c) { if ($a -eq $null) { $b } else { $c } }
$s = IfNull $myval "new value" $myval
$x = IfNull $myval "" $otherval
As you can see a very simple function can give you quite a bit of freedom of syntax.
UPDATE: One extra option to consider in the mix is a more generic IsTrue function:
function IfTrue($a, $b, $c) { if ($a) { $b } else { $c } }
$x = IfTrue ($myval -eq $null) "" $otherval
Then combine that is Powershell's ability to declare aliases that look a bit like operators, you end up with:
New-Alias "??" Coalesce
$s = ?? $myval "new value"
New-Alias "?:" IfTrue
$ans = ?: ($q -eq "meaning of life") 42 $otherval
Clearly this isn't going to be to everyone's taste, but may be what you're looking for.
As Thomas notes, one other subtle difference between the C# version and the above is that C# performs short-circuiting of the arguments, but the Powershell versions involving functions/aliases will always evaluate all arguments. If this is a problem, use the if
expression form.
这篇关于Powershell 中的空值合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!