我应该创建一个新数组还是使用array.clear? [英] Should I create a new array or use array.clear?
本文介绍了我应该创建一个新数组还是使用array.clear?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个不定期清零的数据数组.为此,我应该实例化一个新数组,还是使用Array.Clear方法?
I have an array of data that gets zeroed out from time to time. To do that, should I instantiate a new array, or use the Array.Clear method?
例如,
int workingSet = new int[5000];
// Other code here
workingSet = new int[5000];
// or
Array.Clear(workingSet, 0, 5000);
推荐答案
创建新数组而不是旧数组时,C#将:
When you make a new array instead of an old one, C# will:
- 使旧数组有资格进行垃圾收集,并最终对其进行分配
- 分配一个新数组
- 用零填充新数组.
当您保留旧数组时,C#将
When you keep an old array, C# will
- 用零填充旧数组.
在其他条件相同的情况下,第二种方法更有效.
Everything else being equal, the second approach is more efficient.
这篇关于我应该创建一个新数组还是使用array.clear?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文