Solved

How to clear byte array?

Posted on 2004-10-03
3
10,503 Views
Last Modified: 2008-01-09
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
Comment
Question by:ssdjgru1
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
AvonWyss earned 125 total points
ID: 12215164
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
 
LVL 10

Expert Comment

by:ptmcomp
ID: 12215426
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
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12215477
Thank you - the points from this Q just made me a Certified C# Guru Expert... ;-)
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question