Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't resize an existing array, however, you can use:</p> <pre><code>Array.Resize(ref arr, newSize); </code></pre> <p>This allocates a new array, copies the data from the old array into the new array, and updates the <code>arr</code> variable (which is passed by-ref in this case). Is that what you mean?</p> <p>However, any <em>other</em> references still pointing at the old array will not be updated. A better option might be to work with <code>List&lt;T&gt;</code> - then you don't need to resize it manually, and you don't have the issue of out-of-date references. You just <code>Add</code>/<code>Remove</code> etc. Generally, you don't tend to use arrays directly very often. They have their uses, but they aren't the default case.</p> <hr> <p>Re your comments;</p> <ul> <li>boxing: <code>List&lt;T&gt;</code> doesn't box. That is one of the points about generics; under the hood, <code>List&lt;T&gt;</code> is a wrapper around <code>T[]</code>, so a <code>List&lt;int&gt;</code> has an <code>int[]</code> - no boxing. The older <code>ArrayList</code> is a wrapper around <code>object[]</code>, so that <em>does</em> box; of course, boxing <a href="http://msmvps.com/blogs/jon_skeet/archive/2008/10/08/why-boxing-doesn-t-keep-me-awake-at-nights.aspx" rel="nofollow noreferrer">isn't as bad as you might assume</a> anyway.</li> <li>workings of <code>Array.Resize</code>; <s>if I recall, it finds the size of <code>T</code>, then uses <code>Buffer.BlockCopy</code> to blit the contents</s> the actual details are hidden by an internal call - but essentially after allocating a new array it is a blit (memcpy) of the data between the two arrays, so it should be pretty quick; note that for reference-types this only copies the reference, not the object on the heap. However, if you are resizing regularly, <code>List&lt;T&gt;</code> would usually be a lot simpler (and quicker unless you basically re-implement what <code>List&lt;T&gt;</code> does re spare capacity to minimise the number of resizes).</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload