Lets say I have this array,
int[] numbers = {1, 3, 4, 9, 2};
How can I delete an element by "name"? , lets say number 4?
Even ArrayList
didn't help to delete?
string strNumbers = " 1, 3, 4, 9, 2";
ArrayList numbers = new ArrayList(strNumbers.Split(new char[] { ',' }));
numbers.RemoveAt(numbers.IndexOf(4));
foreach (var n in numbers)
{
Response.Write(n);
}
Balabaster's answer is correct if you want to remove all instances of the element. If you want to remove only the first one, you would do something like this:
You can also convert your array to a list and call remove on the list. You can then convert back to your array.
I posted my solution here.
This is a way to delete an array element without copying to another array - just in frame of the same array instance:
You can do in this way: