我有些困难,想要移除数组的第一行(项目ID)。
$test.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
为了列出我尝试的所有选项,我使用了,$test | gm
,它明确说明:
Remove Method void IList.Remove(System.Object value)
RemoveAt Method void IList.RemoveAt(int index)
所以当我尝试$test.RemoveAt(0)
时,我会得到错误:
Exception calling "RemoveAt" with "1" argument(s): "Collection was of a fixed size."At line:1 char:1
+ $test.RemoveAt(1)
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
所以我最终发现这里我的数组需要是
System.Object
类型才能使用$test.RemoveAt(0)
。在脚本开头声明所有数组为列表是最佳实践吗?还是在需要此功能时将数组转换为列表,例如使用$collection = ({$test}.Invoke())
?这两种类型的优缺点是什么?感谢您的帮助。
$arr = @(1,2,3,1); $arr = $arr -ne $arr[0]; Write-Host $arr
返回的是@(2,3)
而不是预期的@(2,3,1)
。 - DarkLite1