PowerShell:为什么真理包含谎言? [英] PowerShell: Why does truth contain lies?

查看:39
本文介绍了PowerShell:为什么真理包含谎言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PS C:\> $true -contains "Lies"
True

...?

help about_operators

比较运算符包括包含运算符(in、-notin、-contains, -notcontains),决定一个测试值是否出现在参考集中.

comparison operators include the containment operators (in, -notin, -contains, -notcontains), which determine whether a test value appears in a reference set.

$true 不是一个集合,它怎么可能是一个参考集"并包含......任何东西?它不包含虚假的东西.

$true is not a collection, how can it be a "reference set" and contain ... anything at all? It doesn't contain falsey things.

同样 $null -in $null 为真

推荐答案

$true 被转换为 1 个元素的集合,所以它等价于:

$true get converted to a collection of 1 element, so it's equivalent to:

# ~> @($true) -contains "Lies"
True

所以集合中的每个元素都被比作谎言":

So each element in the collection gets compared to "Lies" :

# ~> $true -eq "Lies"
True

所以真正的问题是,为什么上述表达式的计算结果为真.发生的事情是它正在将操作的右侧转换为匹配左侧的类型:

So the real question is, why does the above expression evaluate to true. What's happening is that it is converting the right hand side of the oepration to match the type of the left hand side:

# ~> $true -eq [Bool]"Lies"
True

因为:

# ~> [Bool]"Lies"
True

所以如果你交换操作:

# ~> "Lies" -eq $true
False

因为:

# ~> [String]$true
True

同理,$null -in $null$null -in @($null)$null -eq $null 相同代码>是真的.

Similarly, $null -in $null is the same as $null -in @($null) and $null -eq $null is true.

这篇关于PowerShell:为什么真理包含谎言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆