Windows Powershell 复制数组

Kersen ·
更新时间:2024-09-21
· 855 次阅读

数组属于引用类型,使用默认的的赋值运算符在两个变量之间赋值只是复制了一个引用,两个变量共享同一份数据。这样的模式有一个弊病如果其中一个改变也会株连到另外一个。所以复制数组最好使用Clone()方法,除非有特殊需求。

PS C:Powershell> $chs=@("A","B","C") PS C:Powershell> $chsBak=$chs PS C:Powershell> $chsBak[1]="H" PS C:Powershell> $chs A H C PS C:Powershell> $chs.Equals($chsBak) True PS C:Powershell> $chsNew=$chs.Clone() PS C:Powershell> $chsNew[1]="Good" PS C:Powershell> $chs.Equals($chsNew) False PS C:Powershell> $chs A H C 您可能感兴趣的文章:Windows Powershell 创建数组Windows Powershell 访问数组Windows Powershell强类型数组Windows Powershell使用哈希表Windows Powershell使用管道Windows Powershell对象转换成文本



需要 登录 后方可回复, 如果你还没有账号请 注册新账号