多维数组初始化似乎对空格敏感

Multidimensional array initialization seems sensitive to whitespace

我注意到这两个声明之间的区别,只有逗号的位置发生变化:

$a = @( @('a','b'),
        @('c','d'))

$b = @( @('a','b')
      , @('c','d'))

在这种情况下,$a.length 的计算结果为 2,$b.length 的计算结果为 3。$b 的第一个子数组已展平。

这是一项功能吗?我在哪里可以找到它的文档?

顺便说一下,$PSVersionTable:

Name                           Value
----                           -----
PSVersion                      4.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.42000
BuildVersion                   6.3.9600.16406
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion      2.2
 , Comma operator
         As a binary operator, the comma creates an array. As a unary
         operator, the comma creates an array with one member. Place the
         comma before the member.

Source.

这是因为 @('a','b') 会将两个字符串 ab 推入数组 $b 而您强制 @('c','d') 推入 $b 作为 array 使用逗号。

示例:

$b = @( @('a','b')
      , @('c','d'))

$b | foreach { Write-Host "Item: $_"}

输出:

Item: a
Item: b
Item: c d

如果你看一下类型:

$b | foreach { $_.GetType()}

你得到:

IsPublic IsSerial Name     BaseType     
-------- -------- ----     --------     
True     True     String   System.Object
True     True     String   System.Object
True     True     Object[] System.Array 

强制$b包含两个arrays,使用逗号二元运算符:

$b = @(@('a','b'),@('c','d'))