468,785 Members | 1,674 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,785 developers. It's quick & easy.

delete an element from an array C#

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[i];
any help would be great....thanks
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...neral/200511/1
Nov 2 '05 #1
3 21322
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 2 '05 #2
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" <u15083@uwe> wrote in message
news:56c48414c4209@uwe...
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[i];
any help would be great....thanks
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...neral/200511/1

Nov 2 '05 #3


"Brian Underhill via DotNetMonster.com" wrote:
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[i];
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.
Nov 2 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Westcoast Sheri | last post: by
4 posts views Thread by Shea Martin | last post: by
3 posts views Thread by Brian Underhill via DotNetMonster.com | last post: by
7 posts views Thread by JH Programmer | last post: by
29 posts views Thread by Jon Slaughter | last post: by
29 posts views Thread by =?Utf-8?B?R2Vvcmdl?= | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.