39834

How to trim an array in .NET?

Say I have an array

array<double>^ buffer = gcnew array<double>(100);

And I want a function that does something like:

void foo(array<double>^% buffer) { Array::Resize(buffer, 10); }

but that don't allocate and/or move &buffer[0] when you want to trim the array.

Answer1:

.NET arrays are immutable in size once created. You <strong>can't</strong> trim it; you must reallocate and copy. So Array.Resize already does everything you need. Perhaps just ignore the elements at the end if you really don't want to do this.

Or; use a List<T>, which encapsulates an array, and does have TrimExcess(). In C# terms:

var list = new List<int>(100); // prints 0/100 Console.WriteLine("{0} / {1}", list.Count, list.Capacity); list.Add(1); list.Add(2); list.Add(3); // prints 3/100 Console.WriteLine("{0} / {1}", list.Count, list.Capacity); list.TrimExcess(); // prints 3/3 Console.WriteLine("{0} / {1}", list.Count, list.Capacity);

Answer2:

You cannot do this in .NET. Arrays in .NET are of fixed size once allocated; the only way you can change the size of an array is to re-allocate it (which is what Array.Resize does), and this will invariably change the location of the array in memory.

Recommend

  • why do STL containers use copying to populate in resize?
  • Open mp4 with libVLC and play it in OpenCV C++
  • Allocation memory error with use struct for c
  • reading large lists through stdin in C
  • How can I pass the index of a for loop as the argument for pthread_create
  • Fortran array automatically growing when adding a value
  • CHAR vs VARCHAR for password security
  • C# “cannot assign field because it is a foreach iteration variable”
  • Excel 2007: Format of email address from Outlook 2007
  • Netlink sockets and libnl - nl_recvmsgs_default returning -16 (EBUSY)
  • Updating and removing unique join relationships in CakePHP
  • Negating Regex PO BOX
  • What is this strange character in chrome's resource css viewer?
  • Is there any way to call saveCurrentTurnWithMatchData without sending a push notification?
  • react split panel resize
  • In C what exactly happens if i use () to initialize a double dimension array instead of the {}?
  • What Makes These Two Array Adds Different?
  • Angular2 - Template reference inside NgSwitch
  • Laravel: Getting Session ID oddly truncates when using foreach
  • Not able to aggregate on nested fields in elasticsearch
  • Yii2: Config params vs. const/define
  • Change multiple background-images with jQuery
  • How to rebase a series of branches?
  • Android screen density dpi vs ppi
  • Get object from AWS S3 as a stream
  • Cross-Platform Protobuf Serialization
  • Incrementing object id automatically JS constructor (static method and variable)
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Validaiting emails with Net.Mail MailAddress
  • Change an a tag attribute in JavaScript based on screen width
  • VBA Convert delimiter text file to Excel
  • Do I've to free mysql result after storing it?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • Free memory of cv::Mat loaded using FileStorage API
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • unknown Exception android
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize