Jump to content
  • Advertisement
Sign in to follow this  
Nodger

[.net] resizing an array in c#

This topic is 4850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to be able to resize an array of bytes in c#, Im reading in an unknown # of bytes from a networkStream object. Im used to working in C so I guess Im looking for an equivalent of the realloc() function Thanks a lot

Share this post


Link to post
Share on other sites
Advertisement
Either use BinaryWriter (probably your best choice, it sounds like), ArrayList, List<byte> (for C# 2.0), or simply resize the array yourself (make a new one and copy it over). Again, BinaryWriter is probably your best choice.

Share this post


Link to post
Share on other sites
I just use the last method that Holy Fuzz suggested:


ArrayType[] oldArray = m_Array;
m_Array = new ArrayType[newSize];
System.Array.Copy(oldArray, m_Array, Min(oldArray.Length, newSize));
oldArray = null;


And that has the advantage of making you see how expensive resizing arrays are, as opposed to VB.NET programmers using ReDim in a loop to increase the array size by 1 each time!

Instead of increasing by a fixed amount, you might want to be clever and increase it by more each time. ArrayList, List<> and StringBuilder double their capacity every time they resize.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!