3

Thanks to PowerShell Logic, I'm familiar with some intricacies of how -eq works when the left operand is a collection - rather than a boolean, -eq will return an array of elements that equal the right operand. From the 3.0 spec:

If the value designated by the left operand is not a collection, the result has type bool. Otherwise, the result is a possibly empty unconstrained 1-dimensional array containing the elements of the collection that test True when compared to the value designated by the right operand.

That said, I've found a case that doesn't quite make sense to me. If I declare an array with an element that is also an array, I can't actually get -eq to match that element:

$a = @( 1, @(2, 3) )
$a -eq 1              # results in an array with a single element, containing 1
$a -eq @( 2, 3 )      # results in an empty array

What's going on? It's not particularly easy to tell if this is PowerShell wrapping mixed-type array elements, wrapping two dimensional arrays, issues with value vs. reference type equality checks, or any subset of those.

4

1 回答 1

2

我相信这与 .NET 中的数组比较仅比较两个对象引用同一个对象,而不是它们具有等效值的事实有关。考虑:

# ~> @(2,3).Equals(@(2,3))
False

# ~> $arr = @(2,3)

# ~> $arr.Equals($arr)
True

# ~> $arr.Equals(@(2,3))
False

# ~> @(1, $arr) -eq $arr
2
3
于 2013-05-04T00:54:25.817 回答