PowerShell:数组表示法之间的区别?
这两个数组创建语句有区别吗?那么,创建数组时“@”符号是可选的吗?
$a = "This", "Is", "a", "cat"
$a.GetType()
$a | gm
$a = @("This", "Is", "a", "cat")
$a.GetType()
$a | gm
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在其他情况下是可选的。
In other case is optional.
虽然我不是 100% 确定(这取决于 PowerShell 的能力),但差异可能如下:
"This", "Is", "a", "cat"
创建一个数组。@("This", "Is", "a", "cat")
创建相同的数组,然后然后对其应用运算符@()(显然是多余的操作在这种特殊情况下)。例如,使用这个profiler 我们可以看到第二个表达式相当慢(14%左右),所以我的猜测可能是正确的。理想情况下,PowerShell 代码解释器可以以相同的方式处理这两个表达式,但事实可能并非如此。
另请参阅帮助主题(最后,关于运算符
@()
和,
)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,
)