In c# does Array.ToArray() perform a DEEP copy? - c#

This should be a pretty basic question, but I've been having a little trouble finding a definite answer.
When you have an array of values and you use the .ToArray() method does it create a deep or shallow copy of the array?

No.
You can easily verify this by writing a small program to test.

Not strictly speaking, ICollection.ToArray() creates a new T[] and assigns each element in the original collection to the new array using Array.CopyTo().
Note:
If T is a value type, values are assigned and not references. This will behave as one would expect of a "Deep" copy.
int[] foo = { 1, 2, 3, 4 };
int[] bar = foo.ToArray();
for (int i = 0; i < foo.Length; i++) foo[i] += 10;
Console.WriteLine(string.Join(',', foo)); // 11, 12, 13, 14
Console.WriteLine(string.Join(',', bar)); // 1, 2, 3, 4

Related

Fast copy from array to structure array in c#

Is there any way to fast-copy an array to a member of another structure array without using a for loop? I want to print out "6" when I run this code.
using System;
public class Program
{
struct output
{
public int A;
public int B;
}
public static void Main()
{
var output_array = new output[10];
var a_array = new int[] { 0, 2, 4, 6 ,8, 10, 12, 14, 16, 18};
var b_array = new int[] { 1, 3, 5, 7 ,9, 11, 13, 15, 17, 19};
// Copy a_array to output[].A
// and copy b_array_to output[].B , without using any loop.
Console.WriteLine(output_array[3].A);
}
}
It's not going to be specifically fast but it is succinct to use the Zip extension method:
var a_array = new int[] { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 };
var b_array = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
var output_array = a_array.Zip(b_array, (a, b) => new output { A = a, B = b }).ToArray();
This obviously creates the array, rather than populating an existing array. That shouldn't be an issue for situations like your example, but maybe it is in some real-world situations. In that case, you could create an array and then Array.Copy the elements over.
EDIT:
Actually, I wonder whether I might have misinterpreted your requirement, in which case creating a new array might be an issue. You have included b_array in your code but a more careful reading of your question and your code seems to suggest that that is irrelevant. Please take care to include ONLY what is relevant to the issue because anything else can cloud the issue and waste everyone's time.
If you specifically want the contents of a_array copied into an existing array and b_array is irrelevant then you can't really do it without a loop but, again, LINQ can help flatten that loop:
var output_array = new output[10];
var a_array = new int[] { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 };
Array.ForEach(Enumerable.Range(0, Math.Min(output_array.Length, a_array.Length)).ToArray(),
i => output_array[i].A = a_array[i]);
I've included that Math.Min call to allow for the two arrays to be different lengths but, if you know they're not, you can do away with that and just use one array. Obviously that ForEach call is masking a loop but the actual you see is more succinct. For a one-line loop though, is it really worth the effort and the fact that you have to generate a int array to do it?
I should also point that, while this looks like it would be a problem with value types:
output_array[i].A = a_array[i]
it actually doesn't. Indexing arrays actually provides direct access to the element, rather than creating a copy, so setting a property of that element is not a problem, even for value types.
The memory layout of a_array and output_array will be different so you can't use any tricks with memory copying the bytes.
There are ways to achieve the copy without using an assign in a for loop, e,g.
for (var i = 0; i < 10; i++)
{
output_array[i].A = a_array[i];
// output_array[i].B = b_array[i];
}
but I doubt anything is going to be faster than that. Of course, I haven't tested and benchmarked it.
Perhaps a better solution is not to copy the bytes at all.

Why can't I use array initialisation syntax separate from array declaration?

I can do this with an integer:
int a;
a = 5;
But I can't do this with an integer array:
int[] a;
a = { 1, 2, 3, 4, 5 };
Why not?
To clarify, I am not looking for the correct syntax (I can look that up); I know that this works:
int[] a = { 1, 2, 3, 4, 5 };
Which would be the equivalent of:
int a = 5;
What I am trying to understand is, why does the code fail for arrays? What is the reason behind the code failing to be recognised as valid?
The reason there is a difference is that the folks at Microsoft decided to lighten the syntax when declaring and initializing the array in the same statement, but did not add the required syntax to allow you to assign a new array to it later.
This is why this works:
int[] a = { 1, 2, 3, 4, 5 };
but this does not:
int[] a;
a = { 1, 2, 3, 4, 5 };
Could they have added the syntax to allow this? Sure, but they didn't. Most likely they felt that this use-case is so seldom used that it didn't warrant prioritizing over other features. All new features start with minus 100 points and this probably just didn't rank high enough on the priority list.
Note that { 1, 2, 3, 4, 5 } by itself has no meaning; it can only appear in two places:
As part of an array variable declaration:
int[] a = { 1, 2, 3, 4, 5 };
As part of an array creation expression:
new int[] { 1, 2, 3, 4, 5 }
The number 5, on the other hand, has a meaning everywhere it appears in C#, which is why this works:
int a;
a = 5;
So this is just special syntax the designers of C# decided to support, nothing more.
This syntax is documented in the C# specification, section 12.6 Array Initializers.
The reason your array example doesn't work is because of the difference between value and reference types. An int is a value type. It is a single location in memory whose value can be changed.
Your integer array is a reference type. It is not equivalent to a constant number of bytes in memory. Therefore, it is a pointer to the bytes where that data is stored.
In this first line, you are assigning null to a.
int[] a;
In the next line, if you want to change the value of the array, you need to assign it to a new array.
a = new[] {1, 2, 3, 4, 5};
That is why you need the new[] before the list of values within the array if you strongly type your declaration.
int[] a = {1, 2, 3, 4, 5}; // This will work.
var a = {1, 2, 3, 4, 5}; // This will not.
However, as many of the other answers have said, if you declare it in a single line, then you do not need the new[]. If you separate the declaration and initialization, then you are required to use new[].
{} syntax is available for array initialization, not to be used after declaration.
To initialize an array you should try like this:
int[] a = { 1, 2, 3, 4, 5 };
Other ways to Initializing a Single-dimensional array:
int[] a = new int[] { 1, 2, 3, 4, 5 };
int[] a = new int[5] { 1, 2, 3, 4, 5 };
Have a look at this: different ways to initialize different kinds of arrays

Fastest way to extend array

I am looking for fastest way to extend an array.
No matter if only for length + 1 or length + x it has to be the most fastest way.
Here is an example:
var arr = new int [200];
for(int = 0; i < 200; i++)
arr[i] = i;
And now I want to extend arr for 5 items beginning at index position 20.
var arr2 = new int [] { 999, 999, 999, 999, 999 }
How do I place arr2 inside arr by using most fast way in terms of performance?
The result shall look like this
0,1,2,3,4....20, 999, 999, 999, 999, 999, 21, 22, 23, 24....199
Create a new array which is the size you want, then use the static Array.Copy method to copy the original arrays into the new one.
You can't "extend" an array, you can only create a bigger one and copy the original into it.
Also, consider using List<int> or LinkedList<> instead of an array, unless you require extremely fine-grained control over what is in memory.
It is far easier to use List. But if you have to use arrays, you have to create new array of size 205 and copy values from both source arrays, since array size is constant.
Your best bet is to use something like List<int> rather than an array. But if you must use an array:
int[] arr1 = new int[200];
// initialize array
int[] arr2 = new int[]{999, 999, 999, 999, 999};
int targetPos = 20;
// resizes the array, copying the items
Array.Resize(ref arr1, arr1.Length + arr2.Length);
// move the tail of the array down
Buffer.BlockCopy(arr1, 4*targetPos, arr1, 4*(targetPos+arr2.Length), 4*(arr1.Length - targetPos));
// copy arr2 to the proper position
Buffer.BlockCopy(arr2, 0, 4*arr1.targetPos, 4*arr2.Length);
It might be faster to create a new array and copy the items, like this.
int[] newArray = new int[arr1.Length + arr2.Length];
// copy first part of original array
Buffer.BlockCopy(arr1, 0, newArray, 0, 4*targetPos);
// copy second array
Buffer.BlockCopy(arr2, 0, newArray, 4*targetPos, 4*arr2.Length);
// copy remainder of original array
Buffer.blockCopy(arr1, 4*targetPos, newArray, 4*(targetPos + arr2.Length), 4*(arr1.Length - targetPos));
// and replace the original array
arr1 = newArray;
Which version is faster will depend on where targetPos is. The second version will be faster when targetPos is small. When targetPos is small, the first version has to copy a lot of data twice. The second version never copies more than it has to.
BlockCopy is kind of a pain to work with because it requires byte offsets, which is the reason for all the multiplications by four in the code. You might be better off using Array.Copy in the second version above. That will prevent you having to multiply everything by 4 (and forgetting sometimes).
If you know how long the array will be dimension it to that length,
var ints = new int[someFixedLength];
If you have a vauge idea of the length, use a generic list.
var ints = new List<int>(someVagueLength);
both types implement IList but, the List type handles the redimensioning of the internal array is generically the "most fast" way.
Note: the initial .Count of the List will be 0 but, the internal array will be dimensioned to size you pass to to the constructor.
If you need to copy data between arrays, the quickest way is Buffer.BlockCopy, so from your example
Buffer.BlockCopy(arr2, 0, arr, sizeof(int) * 20, sizeof(int) * 5);
copies all 5 ints from arr2 into indecies 20, 21 ... 24 of arr.
there is no faster way to do this with c# (currently).
An answer showing timing benchmarks is given here: Best way to combine two or more byte arrays in C# . If you consider the "array you insert into " as arrays 1 and 3, and the "array to be inserted" as array 2, then the "concatenate three arrays" example applies directly.
Note the point at the end of the accepted answer: the method that is faster at creating yields an array that is slower to access (which is why I asked if you cared about speed to create, or access speed).
using System.Linq you can do the following to extend an array by adding one new object to it...
int[] intA = new int[] { 1, 2, 3 };
int intB = 4;
intA = intA.Union(new int[] { intB }).ToArray();
...or you can extend an array by adding another array of items to it...
int[] intA = new int[] { 1, 2, 3 };
int[] intB = new int[] { 4, 5, 6 };
intA = intA.Union(intB).ToArray();
...or if you don't care about duplicates...
int[] intA = new int[] { 1, 2, 3 };
int[] intB = new int[] { 4, 5, 6 };
intA = intA.Concat(intB).ToArray();

Fastest way to chop array in two pieces

I have an array, say:
var arr1 = new [] { 1, 2, 3, 4, 5, 6 };
Now, when my array-size exceeds 5, I want to resize the current array to 3, and create a new array that contains the upper 3 values, so after this action:
arr1 = new [] { 1, 2, 3 };
newArr = new [] { 4, 5, 6 };
What's the fastest way to do this? I guess I'll have to look into the unmanaged corner, but no clue.
Some more info:
The arrays have to be able to size up without large performance hits
The arrays will only contain Int32's
Purpose of the array is to group the numbers in my source array without having to sort the whole list
In short: I want to split the following input array:
int[] arr = new int[] { 1, 3, 4, 29, 31, 33, 35, 36, 37 };
into
arr1 = 1, 3, 4
arr2 = 29, 31, 33, 35, 36, 37
but because the ideal speed is reached with an array size of 3, arr2 should be split into 2 evenly sized arrays.
Note
I know that an array's implementation in memory is quite naive (well, at least it is in C, where you can manipulate the count of items in the array so the array resizes). Also that there is a memory move function somewhere in the Win32 API. So I guess this would be the fastest:
Change arr1 so it only contains 3 items
Create new array arr2 with size 3
Memmove the bytes that aren't in arr1 anymore into arr2
I'm not sure there's anything better than creating the empty arrays, and then using Array.Copy. I'd at least hope that's optimized internally :)
int[] firstChunk = new int[3];
int[] secondChunk = new int[3];
Array.Copy(arr1, 0, firstChunk, 0, 3);
Array.Copy(arr1, 3, secondChunk, 0, 3);
To be honest, for very small arrays the overhead of the method call may be greater than just explicitly assigning the elements - but I assume that in reality you'll be using slightly bigger ones :)
You might also consider not actually splitting the array, but instead using ArraySegment to have separate "chunks" of the array. Or perhaps use List<T> to start with... it's hard to know without a bit more context.
If speed is really critical, then unmanaged code using pointers may well be the fastest approach - but I would definitely check whether you really need to go there before venturing into unsafe code.
Are you looking for something like this?
static unsafe void DoIt(int* ptr)
{
Console.WriteLine(ptr[0]);
Console.WriteLine(ptr[1]);
Console.WriteLine(ptr[2]);
}
static unsafe void Main()
{
var bytes = new byte[1024];
new Random().NextBytes(bytes);
fixed (byte* p = bytes)
{
for (int i = 0; i < bytes.Length; i += sizeof(int))
{
DoIt((int*)(p + i));
}
}
Console.ReadKey();
}
This avoids creating new arrays (which cannot be resized, not even with unsafe code!) entirely and just passes a pointer into the array to some method which reads the first three integers.
If your array will always contain 6 items how about:
var newarr1 = new []{oldarr[0], oldarr[1],oldarr[2]};
var newarr2 = new []{oldarr[3], oldarr[4],oldarr[5]};
Reading from memory is fast.
Since arrays are not dynamically resized in C#, this means your first array must have a minimum length of 5 or maximum length of 6, depending on your implementation. Then, you're going to have to dynamically create new statically sized arrays of 3 each time you need to split. Only after each split will your array items be in their natural order unless you make each new array a length of 5 or 6 as well and only add to the most recent. This approach means that each new array will have 2-3 extra pointers as well.
Unless you have a known number of items to go into your array BEFORE compiling the application, you're also going to have to have some form of holder for your dynamically created arrays, meaning you're going to have to have an array of arrays (a jagged array). Since your jagged array is also statically sized, you'll need to be able to dynamically recreate and resize it as each new dynamically created array is instantiated.
I'd say copying the items into the new array is the least of your worries here. You're looking at some pretty big performance hits as well as the array size(s) grow.
UPDATE: So, if this were absolutely required of me...
public class MyArrayClass
{
private int[][] _master = new int[10][];
private int[] _current = new int[3];
private int _currentCount, _masterCount;
public void Add(int number)
{
_current[_currentCount] = number;
_currentCount += 1;
if (_currentCount == _current.Length)
{
Array.Copy(_current,0,_master[_masterCount],0,3);
_currentCount = 0;
_current = new int[3];
_masterCount += 1;
if (_masterCount == _master.Length)
{
int[][] newMaster = new int[_master.Length + 10][];
Array.Copy(_master, 0, newMaster, 0, _master.Length);
_master = newMaster;
}
}
}
public int[][] GetMyArray()
{
return _master;
}
public int[] GetMinorArray(int index)
{
return _master[index];
}
public int GetItem(int MasterIndex, int MinorIndex)
{
return _master[MasterIndex][MinorIndex];
}
}
Note: This probably isn't perfect code, it's a horrible way to implement things, and I would NEVER do this in production code.
The obligatory LINQ solution:
if(arr1.Length > 5)
{
var newArr = arr1.Skip(arr1.Length / 2).ToArray();
arr1 = arr1.Take(arr1.Length / 2).ToArray();
}
LINQ is faster than you might think; this will basically be limited by the Framework's ability to spin through an IEnumerable (which on an array is pretty darn fast). This should execute in roughly linear time, and can accept any initial size of arr1.

C# - List member needs deep copy?

I have class with a List<int> member. If I want to clone an instance of this class, do I need a deep copy or the MemberwiseClone() shallow copy is enough?
We need a deep copy if at least one member is a reference to an object, right? Does that mean having List, DateTime, String, MyClass, ..., will need a deep copy?
This entirely depends on how you plan to use the copy.
If you do a shallow copy like
List<int> x = new List<int>() { 1, 2, 3, 4, 5 };
List<int> y = x;
y[2] = 4;
Then x will contain {1, 2, 4, 4, 5 }
If you do a deep copy of the list:
List<int> x = new List<int> { 1, 2, 3, 4, 5 };
List<int> y = new List<int>(x);
y[2] = 4;
Then x will contain { 1, 2, 3, 4, 5 } and y will contain { 1, 2, 4, 4, 5 }
How you plan on using the copy really determines whether you use shallow or deep copies.
If you have List<int> originalList = new List{1, 2}
then doing this:
List<int> newList = new List<int>();
foreach(int i in originalList)
newList.Add(i);
will get you a cloned list.
However, if you tried what I did above with a List generic on some reference type, then you would not succeed. After altering one of the objects in your list, you would see the altered version whether referencing it from the original list or the new list.
Short answer, yes.
Long answer, you need to determine what the copy in your case actually means with respect to the list. Do you need a copy of the contents of the list as well? For value types like ints, its pretty straight forward, if it were reference types though....yo have some questions to ask regarding what you want your list to contain.

Categories

Resources