Arrays 如何声明字符串数组(在多行上)
为什么Arrays 如何声明字符串数组(在多行上),arrays,powershell,Arrays,Powershell,为什么$dlls.Count返回单个元素?我尝试将我的字符串数组声明为: $basePath = Split-Path $MyInvocation.MyCommand.Path $dlls = @( $basePath + "\bin\debug\dll1.dll", $basePath + "\bin\debug\dll2.dll", $basePath + "\bin\debug\dll3.dll" ) 我发现了,我必须用分号而不是逗号…谁能解释为什么 根据几乎所
$dlls.Count
返回单个元素?我尝试将我的字符串数组声明为:
$basePath = Split-Path $MyInvocation.MyCommand.Path
$dlls = @(
$basePath + "\bin\debug\dll1.dll",
$basePath + "\bin\debug\dll2.dll",
$basePath + "\bin\debug\dll3.dll"
)
我发现了,我必须用分号而不是逗号…谁能解释为什么 根据几乎所有的来源(例如)它显然是逗号
您应该使用以下内容:
$dlls = @(
($basePath + "\bin\debug\dll1.dll"),
($basePath + "\bin\debug\dll2.dll"),
($basePath + "\bin\debug\dll3.dll")
)
or
$dlls = @(
$($basePath + "\bin\debug\dll1.dll"),
$($basePath + "\bin\debug\dll2.dll"),
$($basePath + "\bin\debug\dll3.dll")
)
正如您的答案所示,分号也起作用,因为它标志着一条语句的结束……这将被计算,类似于使用括号
或者,使用另一种模式,如:
$dlls = @()
$dlls += "...."
但是,您可能希望使用ArrayList并获得性能优势
请参见您正在梳理路径,因此请使用cmdlet: 您不需要使用任何逗号、分号或括号。
另请参见。可能的ah副本谢谢。为什么逗号是可选的?为什么powershell似乎没有任何一致的语法?这更令人困惑lol@ibiza我不认为它们是可选的,因为这意味着拥有它们也会起作用,但事实并非如此。因此,不使用逗号是强制性的(除非您像其他答案中所示将条目括在括号中)。所有这些方法都有效:使用逗号、分号或简单的换行符。(在PowerShell 6.2中测试)
$dlls = @()
$dlls += "...."
$dlls = @(
Join-Path $basePath '\bin\debug\dll1.dll'
Join-Path $basePath '\bin\debug\dll2.dll'
Join-Path $basePath '\bin\debug\dll3.dll'
)