delete an element from an array C#

  • Thread starter Brian Underhill via DotNetMonster.com
  • Start date
B

Brian Underhill via DotNetMonster.com

I am trying to delete an element from an array. I have an array of 52
elements and I want to search for an element and delete it. Therefore,
making it an array of 51 elements.

is it just

delete MyArray;


any help would be great....thanks
 
M

Mattias Sjögren

I am trying to delete an element from an array. I have an array of 52
elements and I want to search for an element and delete it. Therefore,
making it an array of 51 elements.

Arrays have a fixed size you set when you allocate them so you canät
do that. You may want to use an ArrayList instead.


Mattias
 
C

Cowboy \(Gregory A. Beamer\)

Change to an ArrayList or similar (SortedList) as arrays are initialized to
a specific size and do not automagically resize because you are tired of one
item.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***********************************************
Think Outside the Box!
***********************************************
 
G

Guest

Brian Underhill via DotNetMonster.com said:
I am trying to delete an element from an array. I have an array of 52
elements and I want to search for an element and delete it. Therefore,
making it an array of 51 elements.

is it just

delete MyArray;


any help would be great....thanks


Once you get onto the latest release containing generics, there is a static
method on the Array class called Resize---

int[] myArray = new int[10];

// populate it

int[] myShorterArray = Array.Resize<int>(9);

// you have just truncated the array by one element.

Now, if you read carefully, all this is really doing is re-allocating the
array and copying the appropriate elements into the new array. this is the
same manual process you could have done pre-generics, but this makes it so
much cleaner.

Not to disagree with the other replies- this is just one more way of looking
at it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top