delete an element from an array C#

  • Thread starter Thread starter Brian Underhill via DotNetMonster.com
  • Start date 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
 
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
 
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!
***********************************************
 
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.
 
Back
Top