Solved

How to clear byte array?

Posted on 2004-10-03
3
10,445 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

947 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now