如何创建不可变数组? [英] How can I create an immutable array?

查看:50
本文介绍了如何创建不可变数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Powershell 中创建一个(不可变的)元组(或数组)元素,所以我的尝试如下:

I want to create an (immutable) tuple (or array) of elements in Powershell, so my attempt is the following:

$t = @("A","B")

现在,这将创建一个我可以添加到的数组:

Now, this creates an array that I can add to:

$t += "C"

我希望 $t 在程序执行期间是不可变的.我该怎么做?

I want $t to be immutable during the execution of the program. How can I do this?

推荐答案

你实际上可以使用元组

PS[1] (203) > $t = [tuple]::create(1,2,3)
PS[1] (204) > $t.item1
1
PS[1] (205) > $t.item1 = 4
'item1' is a ReadOnly property.
At line:1 char:1
+ $t.item1 = 4
+ ~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

这篇关于如何创建不可变数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆