PowerShell equality operator not a symmetric relation?

前端 未结 4 464
面向向阳花
面向向阳花 2021-01-17 21:00

Can someone please explain to me why the equality operator in PowerShell is not a symmetric relation??

PS> \"\" -eq 0
False
PS> 0 -eq \"\"
True
         


        
4条回答
  •  长情又很酷
    2021-01-17 21:16

    My first guess is that in the first case, there is a conversion of the right hand operand from 0 to "0" (the type of the left hand operator being a string), therefore "" is not equal to "0". In the second case, there is a conversion from "" to int, and "" is considered as being 0.

    Try 3 + ""

提交回复
热议问题