C# 如何用另一个数组创建和初始化一个数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9917390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create and initialize an array with another array?
提问by Ashwin Nanjappa
To create and initialize an array with another array I currently do this:
要使用另一个数组创建和初始化数组,我目前这样做:
void Foo( int[] a )
{
int[] b = new int[ a.Length ];
for ( int i = 0; i < a.Length; ++i )
b[ i ] = a[ i ];
// Other code ...
}
Is there a shorter or more idiomatic way of doing this in C#?
在 C# 中是否有更短或更惯用的方法来做到这一点?
It will be great if this can be done in a single statement, like in C++:
如果这可以在单个语句中完成,就像在 C++ 中一样,那就太好了:
vector<int> b( a );
If this cannot be done in a single statement, I will take what I get :-)
如果这不能在一个声明中完成,我会接受我得到的:-)
采纳答案by Reed Copsey
I like using LINQ for this:
我喜欢为此使用 LINQ:
int[] b = a.ToArray();
That being said, Array.Copy does have better performance, if this will be used in a tight loop, etc:
话虽如此,Array.Copy 确实具有更好的性能,如果这将用于紧密循环等:
int[] b = new int[a.Length];
Array.Copy(a, b, a.Length);
Edit:
编辑:
It will be great if this can be done in a single statement, like in C++:
vector b( a );
如果这可以在单个语句中完成,就像在 C++ 中一样,那就太好了:
向量 b( a );
The C# version of this would be:
这个 C# 版本将是:
List<int> b = new List<int>(a);
List<T>is C#'s equivalent to std::vector<T>. The constructor aboveworks with any IEnumerable<T>, including another List<T>, an array (T[]), etc.
List<T>是 C# 的等效于std::vector<T>. 上面的构造函数适用于 any IEnumerable<T>,包括 another List<T>、数组 ( T[]) 等。
回答by Emmanuel N
Use Array.Copyto copy an array
使用Array.Copy复制数组
int[] source = new int[5];
int[] target = new int[5];
Array.Copy(source, target, 5);
回答by Kamrul
Also try the default Clone()function which is implemented from the IClonableinterface.
还可以尝试Clone()从IClonable接口实现的默认功能。
int[] b = a.Clone() as int[];
回答by DinoM
Clone() and ToArray() are syntactically nice because you don't need to pre-allocate a destination array, but in terms of performance, Array.Copy() is the fastest method (see caveat below).
Clone() 和 ToArray() 在语法上很好,因为您不需要预先分配目标数组,但就性能而言,Array.Copy() 是最快的方法(请参阅下面的警告)。
The reason for Array.Copy() being so fast is that it doesn't allocate any memory. However, if you require your arrays to be copied to a new region of memory each time, then Array.Copy() is no longer the fastest method.
Array.Copy() 如此之快的原因是它没有分配任何内存。但是,如果您需要每次都将数组复制到新的内存区域,那么 Array.Copy() 不再是最快的方法。
Here are my performance results:
以下是我的表现结果:
Copy: 0 ms
Copy (with allocation): 449 ms
Clone: 323 ms
ToArray: 344 ms
And here's the code I used:
这是我使用的代码:
const int arrayLength = 100000;
const int numberCopies = 1000;
var a = new int[arrayLength];
var b = new int[arrayLength];
var stopwatch = new Stopwatch();
for (var i = 0; i < numberCopies; i++) {
Array.Copy(a, b, arrayLength);
}
Console.WriteLine($"Copy: {stopwatch.ElapsedMilliseconds} ms");
stopwatch.Restart();
for (var i = 0; i < numberCopies; i++) {
var c = new int[arrayLength];
Array.Copy(a, c, arrayLength);
}
Console.WriteLine($"Copy (with allocation): {stopwatch.ElapsedMilliseconds} ms");
stopwatch.Restart();
for (var i = 0; i < numberCopies; i++) {
b = (int[]) a.Clone();
}
Console.WriteLine($"Clone: {stopwatch.ElapsedMilliseconds} ms");
stopwatch.Restart();
for (var i = 0; i < numberCopies; i++) {
b = a.ToArray();
}
Console.WriteLine($"ToArray: {stopwatch.ElapsedMilliseconds} ms");

