2

I'm trying to remove last two lines from list, with text document I did it this way:

var lines = System.IO.File.ReadAllLines(doc.txt); 
System.IO.File.WriteAllLines(doc.txt, lines.Take(lines.Length - 2).ToArray());

but when I'm trying to do the same with list

 List<string> myList = new List<string>();

this way, I have not result:

myList.RemoveAt(myList.Count - 2);
nikorio
  • 671
  • 4
  • 16
  • 28

3 Answers3

7

You can use RemoveRange, also note index is zero-based, and Count is not.

list.RemoveRange(list.Count - 2, list.Count - 1);

This will remove last two lines inside the List<string>

abdul
  • 1,562
  • 1
  • 17
  • 22
4

The RemoveAt Method gets an index as parameter. So if you execute this code:

list.RemoveAt(myList.Count - 2);

You will remove the item on position myList.Count - 2 instead of 2 items. So if you want to delete the last two items you can do the following:

list.RemoveAt(list.Count - 1);
list.RemoveAt(list.Count - 1);

Or the better approach is to use RemoveRange as abdul suggest in his answer.

Sebi
  • 3,879
  • 2
  • 35
  • 62
  • 1
    sorry had a typo, did you mean to have list.Count - 2 in your recommendation? wouldn't that result in removing the last line and essentially the 3rd line from the original list? – Hack Jan 05 '17 at 19:52
  • 1
    @Hack thank you hack I missed that in the hurry :-D. You are right. I correct that. – Sebi Jan 05 '17 at 19:56
  • 1
    @Sebi Hello, yes it is useful for my case – nikorio Jan 05 '17 at 22:08
-2

A List<T> is not meant to be indexed by element. You need either an array or a Dictionary to do that.

So, when you have list with all the lines, you will need to find lines by content... or better yet in your example, keep those last 2 lines from being added to the list in the first place, during the IO reading logic.

Aaron Thomas
  • 5,054
  • 8
  • 43
  • 89
  • 1
    The reason why I recommend steering away from using `List` for this is because it is not a collection that is meant to be used by index (ie "last two", "last", "first", etc) – Aaron Thomas Jan 05 '17 at 19:47
  • 2
    A list is absolutely meant to be accessed by index. Under the covers it's just an array that is resized when needed. – juharr Jan 05 '17 at 19:49
  • 1
    @juharr not by the user it's not. `List` is a dynamic collection, not guaranteed to be sorted or indexed in any particular way at a particular time. If you want to do logic involving accessing elements by index (ie "last two"), then an array is what you want to work with. Or something with a key, such as a `Dictionary`. – Aaron Thomas Jan 05 '17 at 19:51
  • 1
    You can determine what the last two items in a list are just as easily as an array, and you can remove them, unlike the array where you can only set them to something else. A dictionary is a completely different beast where you want access via a key and order is not guaranteed. – juharr Jan 05 '17 at 19:53
  • 3
    @AaronThomas I don't think you are right here. The Framework won't give you access to list[index] or list.RemoveAt or list.IndexOf etc. if you shouldn't access a list via index. It's common to access a list via index if needed. – Sebi Jan 05 '17 at 19:54
  • 3
    A `List` is a wrapper around an array of `T`, period. https://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs,cf7f4095e4de7646 – fjardon Jan 05 '17 at 19:55
  • 1
    I stand corrected - esp reading this helped me understand: http://stackoverflow.com/questions/14324987/why-isnt-array-a-generic-type/15561100#15561100 – Aaron Thomas Jan 05 '17 at 20:11