I need to delete the first element of an array.
I have: Numero[10]
.
How do I exclude the value in Numero[0]
and overwrite the other elements?
I need to delete the first element of an array.
I have: Numero[10]
.
How do I exclude the value in Numero[0]
and overwrite the other elements?
I need to delete the first element of an array
You can not delete a position or element from an array. If the array has 10 positions, it will always have 10 positions.
Use the Clear()
method of class Array
to reset this array position to its default value . For example:
int[] seuArray = new int[3];
seuArray[0] = 1;
seuArray[1] = 2;
seuArray[2] = 3;
Array.Clear(seuArray, 0, 1);
foreach (var x in arr) {
Console.Write(x);
}
Result:
023
Being:
seuArray
: the array in question 0
: From which position you want to "clean" 1
: How many positions you want to clean To overwrite the other positions just do:
seuArray[1] = 5;
seuArray[2] = 6;
var foos = new List<Foo>(array);
foos.RemoveAt(index);
return foos.ToArray();
You can try using extension method:
public static T[] RemoveAt<T>(this T[] source, int index)
{
T[] dest = new T[source.Length - 1];
if( index > 0 )
Array.Copy(source, 0, dest, 0, index);
if( index < source.Length - 1 )
Array.Copy(source, index + 1, dest, index, source.Length - index - 1);
return dest;
}
Using this:
Foo[] bar = GetFoos();
bar = bar.RemoveAt(2);
Source: link