Example:
string[] x = {"3","2","1"};
I want to take only the "2"
item from the array x
, resulting in:
x = {"3","1"};
Example:
string[] x = {"3","2","1"};
I want to take only the "2"
item from the array x
, resulting in:
x = {"3","1"};
Turn the vector into a List<T>
and use the method RemoveAt(int)
.
string[] x = {"3","2","1"};
var lista = x.ToList(); // cria um objeto do tipo List<string> a partir do vetor
lista.RemoveAt(1); // remove o item na posição 1
x = lista.ToArray(); // recria o vetor a partir da lista
If you do not know the position of the element to be deleted, do so:
string[] x = {"3","2","1"};
int[] lista = x.ToList(); // cria o objeto do tipo List<string> a partir do vetor
lista.Remove("2"); // remove a primeira ocorrência do elemento que for equivalente a "2"
x = lista.ToArray()l // recria o vetor a partir da lista
Sometimes you can analyze your code and see if there is even the need to use an array. Often use an implementation of IEnumerable
. , such as List<T>
, makes it much easier.
It is very specific that there is a need to use an array. If you need to add and remove items, you should use a List<T>
. , for example. Resizing arrays is somewhat costly.
I'll leave "Arrays considered somewhat harmful" by Eric Lippert as a reading recommendation.