我有些困难,想要移除数组的第一行(项目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())
?这两种类型的优缺点是什么?感谢您的帮助。
$rest
具有一致的数据类型,则此方法不可靠。如果$arr
仅有2个元素,则$rest
将是标量,否则可能是数组甚至是null。 - coldfix$rest
已经是一个数组类型,否则它将保持为一个非数组类型。所以$first,$arr = $arr
应该可以正常工作。 你也可以通过以下方式强制转换:$first,[object[]]$rest = $arr
。 - Willem M.