Remove element from the list in C#

Asked

Viewed 319 times

1

I have a list and I want that after the foreach from that list, remove all elements from it, the method RemoveAll() asks parameter, but I don’t know which parameter to pass.

foreach (var lista in ListaContatos)
{
    lista.Con_codigo = agenciaModel.Bcx_codigo;
    lista.Con_tpcadastro = Con_tpcadastro;
    var stringContent2 = new StringContent(JsonConvert.SerializeObject(lista), Encoding.UTF8, "application/json");
    using (HttpResponseMessage response = await httpClient.PostAsync(UrlApi2, stringContent2))
    { 
        response.EnsureSuccessStatusCode();
    }
}
ListaContatos.RemoveAll();
  • Some difficulty in using the method Clear()?

1 answer

6


The method RemoveAll() exists to pass a condition so will remove all elements that meets a certain condition, is not to erase all of truth. He’s actually pretty inefficient, but if that’s what you need, it’s what you have to use. If you want to remove all elements even without any condition the correct method is the Clear() which is even as efficient as possible.

Documentation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.