[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 11267
  • Last Modified:

How to clear byte array?

Is there any elegant way to empty an array in c#? I noticed that in the past there was a .clear method, but now notihing like that exists. (The only way to empty an array I found out is to create new variable which is empty and assign it ot the old one (and hope the old one will be garbage collected))

Thank you!
0
ssdjgru1
Asked:
ssdjgru1
  • 2
1 Solution
 
AvonWyssCommented:
Lists have .Clear() methods, but Arrays do not. You need to overwrite each single value if you want to reset the array. Your way of assigning a newly created array is just fine; also, most of the time there is no need at all to have the array initialized in the first place!
0
 
ptmcompCommented:
You can replace the old array by an empty array:

// initialize array:
string[] myArray = new string[]{"Hello", "World!"}:

// 'clear' array:
myArray = new string[0];  // the old instance will be deleted by the garbage collection
0
 
AvonWyssCommented:
Thank you - the points from this Q just made me a Certified C# Guru Expert... ;-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now