0

Just out of curiosity, coming from C background, I knew that Array cannot be resized.

However, while I see in C#, it's so easy to do like below

var arr = new int[] {1,2,4,6};
arr = new int[2];

Also, there is a method available

Array.Resize(ref arr, 10);

How is it possible?

Thanks!

Kgn-web
  • 7,047
  • 24
  • 95
  • 161
  • Array.Resize. allocates a new array. It then copies existing element values to the new array. – James Cooke Aug 12 '20 at 14:41
  • C++ has variable-length arrays too, `std::vector`. The equivalent in .NET is `List`, not arrays. In *both* languages, the data is stored in an internal buffer. When the buffer runs out, *both* languages will allocate a new larger buffer (array) and copy the data to it, discarding the older buffer – Panagiotis Kanavos Aug 12 '20 at 14:42
  • Does this answer your question? [change array size](https://stackoverflow.com/questions/4840802/change-array-size) – Pranav Hosangadi Aug 12 '20 at 14:43
  • It's explained in the docs: https://learn.microsoft.com/en-us/dotnet/api/system.array.resize?view=netcore-3.1 "This method allocates a new array with the specified size, copies elements from the old array to the new one" – Christoph Lütjen Aug 12 '20 at 14:44
  • Maybe I misunderstood but did you say that this code `arr = new int[2];` resizes the array? Edit: Okay, after reading the answers, it really seems you actually meant this, at least everyone understood it like this. – Rand Random Aug 12 '20 at 14:46

4 Answers4

3

These operations aren't resizing the array. They're creating a new array of a new size.

Note in the first example that you call new twice. So you're creating two arrays.

In the second example, the documentation explains the same:

This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one.

David
  • 208,112
  • 36
  • 198
  • 279
1

Arrays cannot be resized in C#.

Your first example assigns a new array to the variable arr, it doesn't resize the existing array.

Also Array.Resize is a misnomer: it actually creates a new array and copies the values.

The clue to that is with the ref keyword, which indicates that Array.Resize will be reassigning to arr.

Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
1

The source code for the Array class can be found here.

This is the relevant portion:

T[] larray = array;                
if (larray == null) {
    array = new T[newSize];
    return;
}

if (larray.Length != newSize) {
    T[] newArray = new T[newSize];
    Array.Copy(larray, 0, newArray, 0,  larray.Length > newSize? newSize : larray.Length);
    array = newArray;
}

As you can see, it allocates a new array and then copies whatever it can from the existing array into the new array.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
0

Each of those examples actually creates a brand new array, copies the items one by one from the old to the new, and updates the reference.

If you have a collection where the size will change over time, you're almost always better off using a generic List<T>.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794