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 :-)
如果这不能在一个单独的声明中完成,我将采取我所得到的:-)
4 个解决方案
#1
15
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:
也就是说,数组。拷贝确实有更好的性能,如果这将在一个紧密循环中使用,等等:
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++:
如果这可以在一个语句中完成,就像在c++中那样:
vector b( a );
向量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 above works with any IEnumerable<T>
, including another List<T>
, an array (T[]
), etc.
List
#2
6
Use Array.Copy to copy an array
使用数组。复制一个数组
int[] source = new int[5];
int[] target = new int[5];
Array.Copy(source, target, 5);
#3
1
Also try the default Clone()
function which is implemented from the IClonable
interface.
还可以尝试从IClonable接口实现的默认克隆()函数。
int[] b = a.Clone() as int[];
#4
1
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.
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");
#1
15
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:
也就是说,数组。拷贝确实有更好的性能,如果这将在一个紧密循环中使用,等等:
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++:
如果这可以在一个语句中完成,就像在c++中那样:
vector b( a );
向量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 above works with any IEnumerable<T>
, including another List<T>
, an array (T[]
), etc.
List
#2
6
Use Array.Copy to copy an array
使用数组。复制一个数组
int[] source = new int[5];
int[] target = new int[5];
Array.Copy(source, target, 5);
#3
1
Also try the default Clone()
function which is implemented from the IClonable
interface.
还可以尝试从IClonable接口实现的默认克隆()函数。
int[] b = a.Clone() as int[];
#4
1
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.
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");