I have one generic list which contains some string value. Sometimes the values will be repeat. And I have to delete the specific values from the List Collection. I tried the following code.
List<string> postalCodes = new List<string> { "A1B", "A2B", "A3B","A2B" };
string currentPostalCode = "A2B";
postalCodes.RemoveAt(postalCodes.IndexOf(currentPostalCode));
But this code removing the item from the position 1, but not from 3. How can I delete from the both positions? Please hep me. Thanks in advance.
postalCodes.RemoveAll(c => c == currentPostalCode);
If you want to use RemoveAt
you have to do it in a loop:
int index;
while((index = postalCodes.IndexOf(currentPostalCode)) != -1)
{
postalCodes.RemoveAt(index);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments