简体   繁体   中英

PowerShell: Difference between array notation?

Is there a difference between these two array creation statements? So, is '@' sign optional when creating arrays?

$a = "This", "Is", "a", "cat"
$a.GetType()
$a | gm
$a = @("This", "Is", "a", "cat")
$a.GetType()
$a | gm
$a = @() # declare an empty array.

$a = @(mysingleitem) # declare an array with a single element

In other case is optional.

Is there a difference between these two array creation statements?

Though I am not 100% sure (it depends on PowerShell guts) the difference may be the following: "This", "Is", "a", "cat" creates an array. @("This", "Is", "a", "cat") creates the same array and then applies the operator @() to it (apparently redundant operation in this particular case).

Using, for example, this profiler we can see that the second expression is quite slower (14% or something), so that my guess may be correct. Ideally, PowerShell code interpretator could treat these two expressions in the same way but it probably does not.

See also the help topic (the end, about operators @() and , )

help about_operators

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM