---
title: "Removing an item from a list c#"  
description: "Removing an item from a list c#"  
author: "Anonymous User"  
published: 2013-09-28  
updated: 2013-09-28  
canonical: https://www.mindstick.com/forum/1555/removing-an-item-from-a-list-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Removing an item from a list c#

I am [having trouble](https://answers.mindstick.com/qa/50524/how-to-fix-iphone-8-that-s-overheating-and-having-trouble-turning-on) with removing an item from a list in c#. My current code is below:

```
for (int i = current.Count - 1; i >= 0; i--)
    {
        foreach (ListItem li in sp_list.Items)
        {
            if (li.Text == current[i].uname)
            {
                current.RemoveAt(i);
            }
        }
    }
```

\
The aim of the code is to [compare](https://www.mindstick.com/forum/2036/propertyinfo-getvalue-unable-to-compare-datetimes) the existing items in a list box with newly added items so I know which items have just been added. So currently I [am getting](https://www.mindstick.com/forum/34179/i-am-getting-error-while-assigning-the-data-to-the-array-list) the current list box items from the [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) (they are stored here, it is a databound list box), entering these into a list and for each of the list items, comparing them with the items in a list box and if they match, [remove](https://yourviews.mindstick.com/story/4554/8-harmful-weeds-to-remove-from-garden) the item from the list.

Therefore, in the end, if I [add two](https://www.mindstick.com/forum/480/add-two-number) new entries, the list should only be storing the two newly added values.

The code does not work as the first item is removed fine, but then, the value of i is greater than current.count - and therefore I get an out of range [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling)!

Could [someone help me](https://answers.mindstick.com/qa/42311/can-someone-help-me-with-this-question-about-political-parties) with this issue? Apologies about the confusing [question](https://www.mindstick.com/blog/23175/how-to-solve-neet-question-paper-in-less-time), it's hard to [explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems)! Thanks

## Replies

### Reply by Anonymous User

Hey Jhon!

Once you've found the matching value, and removed it from the list, you want to break out of the inner loop to check the next item.

```
        if (li.Text == current[i].uname)
        {
            current.RemoveAt(i);
            break;
        }
```


---

Original Source: https://www.mindstick.com/forum/1555/removing-an-item-from-a-list-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
