如何以及何时放弃在 C# 中使用数组? [英] How and when to abandon the use of arrays in C#?

查看:30
本文介绍了如何以及何时放弃在 C# 中使用数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直被告知向数组添加元素是这样发生的:

I've always been told that adding an element to an array happens like this:

数组+1元素的空副本是创建,然后从数据然后将原始数组复制到其中新元素的新数据是然后加载

An empty copy of the array+1element is created and then the data from the original array is copied into it then the new data for the new element is then loaded

如果这是真的,那么由于内存和 CPU 利用率的原因,在需要大量元素活动的场景中使用数组是禁忌的,对吗?

If this is true, then using an array within a scenario that requires a lot of element activity is contra-indicated due to memory and CPU utilization, correct?

如果是这样,当您要添加很多元素时,您是否应该尽量避免使用数组?你应该改用 iStringMap 吗?如果是这样,如果您需要两个以上的维度并且需要添加大量元素,会发生什么情况.您是直接降低性能还是应该使用其他东西?

If that is the case, shouldn't you try to avoid using an array as much as possible when you will be adding a lot of elements? Should you use iStringMap instead? If so, what happens if you need more than two dimensions AND need to add a lot of element additions. Do you just take the performance hit or is there something else that should be used?

推荐答案

将通用 List 视为数组的替代.它们支持数组所做的大部分事情,包括根据需要分配初始存储大小.

Look at the generic List<T> as a replacement for arrays. They support most of the same things arrays do, including allocating an initial storage size if you want.

这篇关于如何以及何时放弃在 C# 中使用数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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