I have an array of Foo objects. How do I remove the second element of the array?
I need something similar to RemoveAt()
but for a regular array.
I have an array of Foo objects. How do I remove the second element of the array?
I need something similar to RemoveAt()
but for a regular array.
Here's how I did it...
If you don't want to use List:
You could try this extension method that I haven't actually tested:
And use it like:
Here is an old version I have that works on version 1.0 of the .NET framework and does not need generic types.
This is used like this:
As usual, I'm late to the party...
I'd like to add another option to the nice solutions list already present. =)
I would see this as a good opportunity for Extensions.
Reference: http://msdn.microsoft.com/en-us/library/bb311042.aspx
So, we define some static class and in it, our Method.
After that, we can use our extended method willy-nilly. =)
Not exactly the way to go about this, but if the situation is trivial and you value your time, you can try this for nullable types.
and later check for null entries in your logic..
LINQ one-line solution:
The
1
in that example is the index of the element to remove -- in this example, per the original question, the 2nd element (with1
being the second element in C# zero-based array indexing).A more complete example:
After running that snippet, the value of
myArray
will be{ "a", "c", "d", "e" }
.