-3

I have the following List:

List<string[]> filmlist = new List<string[]>();

The List contains Arrays like

string[] row = { "0", "1", "2", "3", "4", "5", "6", Convert.ToString(filmcounter) };

I want to delete an item in this List with a specific value of filmcounter.

Thanks!

  • in fact you want to delete an element from a string[] - google for that. Second, its not at all clear what you mean, show the list before and after the delete that you want – pm100 Jan 18 '16 at 17:05
  • 1
    I think you are better suited with a `List>` instead. Then you can use the `.Remove()` or `.RemoveAt()` methods. – John Alexiou Jan 18 '16 at 17:06
  • 1
    I agree with the above use a list instead of array, but if you need to use an array google! Find the index of your value and remove that item here is a link http://stackoverflow.com/questions/457453/remove-element-of-a-regular-array – Marko Jan 18 '16 at 17:07
  • Thanks @ja72! I will try this. – mathis.plewa Jan 18 '16 at 17:13
  • Do you wish to remove the filmcounter value from the row itself, or do you wish to remove the entire row that contains that filmcounter value from the filmlist? – TVOHM Jan 18 '16 at 17:22
  • @TVOHM The entire row. So that filmlist.count lowers. – mathis.plewa Jan 18 '16 at 18:06

2 Answers2

0

If, like your additional comment clarifies, you want to delete a whole row that contains a specific 'filmcounter', you can do a few different things.

So using this example data:

List<string[]> filmlist = new List<string[]>()
{
    new[] { "0", "1", "2", "3", "4", "5", "6" },
    new[] { "0", "1", "2", "3", "4", "5", "6" },
    new[] { "0", "1", "2", "3", "4", "5", "7" },
};
string filmcounter = "6";

To delete all rows that contain the specified filmcounter value:

filmlist.RemoveAll(x => x.Contains(filmcounter));

To only delete the first row that contains the specific filmcounter value:

int index = filmlist.FindIndex(x => x.Contains(filmcounter));
if (index != -1)
    filmlist.RemoveAt(index);

FindIndex in the second example will return the first index that contains the filmcounter value. We can then use this index to remove that row. It will return -1 if there are no matches.

TVOHM
  • 2,740
  • 1
  • 19
  • 29
-3
foreach(String value in filmlist){
    filmlist.Remove(value);
}

Check the C# method Remove(), it's exacly what you want.

thomashoareau
  • 99
  • 1
  • 1
  • 10