如何在 C# 中声明可写的常量对象数组?

发布于 2024-10-02 15:16:23 字数 40 浏览 3 评论 0原文

我需要能够设置数组中的对象,但我不希望能够更改任何单个对象的状态。

I need to be able to set the objects in the array, but I don't want to be able to change the state of any of the individual objects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

脸赞 2024-10-09 15:16:23

恐怕你不能。 C# 中没有与 C++ 中的灵活“const”等效的东西。

如果它是您自己的类型,您可以尝试使其不可变(如 string)。这对于更改非常有效:)

请注意,虽然您的问题要求一个可写数组,但实际上没有其他类型 - 您无法创建一个在最初填充时为只读的数组...您必须使用其他一些数组方法(例如,创建一个ReadOnlyCollection来包装另一个包装器已知的集合)。我知道这对于您的具体情况来说不是问题,但我只是想指出这一点。如果您使变量引用数组只读,则只会阻止其他代码将变量的值设置为对另一个数组的引用 - 它不会阻止数组本身内的更改。

You can't, I'm afraid. There's no equivalent of the flexible "const" from C++ in C#.

If it's your own type, you could try to make it immutable (like string) to start with though. That would be pretty effective against changes :)

Note that although your question asks for a writable array, there's actually no other kind - you can't create an array which is read-only when initially populated... you have to use some other approach (e.g. creating a ReadOnlyCollection<T> wrapping another collection which is only known to the wrapper). I know this isn't a problem in your particular case, but I just thought I'd point it out. If you make the variable referring to the array readonly, that only prevents other code from setting the value of the variable to a reference to another array - it doesn't prevent changes within the array itself.

離殇 2024-10-09 15:16:23

好吧,您可以声明一个私有变量,然后声明一个引用该变量的属性,仅定义 getter,这样就使其只读。

例子:

private int[] myArray =new int[5];
public int[] MyArray
{
  get { return MyArray; }
}

Well, you can declare a private variable and then declare a property referencing the variable with only getter defined, that sort of makes it read only.

Example:

private int[] myArray =new int[5];
public int[] MyArray
{
  get { return MyArray; }
}
甜心小果奶 2024-10-09 15:16:23

我会考虑使用某种形式的不可变集合作为更好的设计概念。

选项包括返回为 IEnumerable、使用 Yield[1] 的 IEnumerable、ReadOnlyCollection...请参阅下面的链接以获得更好的解释。

[1]

public IEnumerable<string> Hosts 
      { 
            get     
             {     
                 foreach (var host in _hosts)
                 {    
                     yield return host;
                 }    
             }
     }

请参阅http ://geekswithblogs.net/BlackRabbitCoder/archive/2010/11/04/c.net-fundamentals-returning-an-immutable-collection.aspx

I would look into using some form of Immutable collection as a better design concept.

Options include returning as IEnumerable, IEnumerable using yield[1], ReadOnlyCollection... See link down below for better explanation.

[1]

public IEnumerable<string> Hosts 
      { 
            get     
             {     
                 foreach (var host in _hosts)
                 {    
                     yield return host;
                 }    
             }
     }

See http://geekswithblogs.net/BlackRabbitCoder/archive/2010/11/04/c.net-fundamentals-returning-an-immutable-collection.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文