Solved

How to clear byte array?

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

19 Experts available now in Live!

Get 1:1 Help Now