.NET 中是否可以使用不可变数组? [英] Are immutable arrays possible in .NET?

查看:26
本文介绍了.NET 中是否可以使用不可变数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能以某种方式将 System.Array 标记为不可变的.当放在 public-get/private-set 后面时,它们不能被添加到,因为它需要重新分配和重新分配,但消费者仍然可以设置他们想要的任何下标:

Is it possible to somehow mark a System.Array as immutable. When put behind a public-get/private-set they can't be added to, since it requires re-allocation and re-assignment, but a consumer can still set any subscript they wish:

public class Immy
{
    public string[] { get; private set; }
}

我认为 readonly 关键字可能会奏效,但没有这样的运气.

I thought the readonly keyword might do the trick, but no such luck.

推荐答案

ReadOnlyCollection<T> 可能就是你要找的.它没有 Add() 方法.

这篇关于.NET 中是否可以使用不可变数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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