Home c# How does Removeat work in List?

How does Removeat work in List?

Author

Date

Category

For example, we have a sheet with five elements (indexes: 0 1 2 3 4). If we delete an element with an index 3, the fourth element will receive index 3 and the length of the sheet will decrease? Also interested in the removal of the element: the compiler frees the memory that the remote element occupied, and how fast? That is, for example, if the user uses a program for several hours and thousands of elements have already been removed from the sheet during this time, will there be any memory problems?


Answer 1, Authority 100%

times you too lazy to write code yourself and run, you can use this:

var lst = new list & lt; int & gt; {5, 6, 7, 8, 9};
for (int i = 0; i & lt; lst.count; i ++)
  Console.WriteLine ("LST [{0}] = {1}", I, LST [I]);
Console.Writeline ();
lst.removeat (3);
for (int i = 0; i & lt; lst.count; i ++)
  Console.WriteLine ("LST [{0}] = {1}", I, LST [I]);

The distribution of elements and indexes after removal is completely transparent.

Regarding memory problems. No, there will be no problems, the garbage collector works quite reliably. Theoretically, problems are possible if your class, whose copies are contained in the List, contains some resources to be released, and you before removeat forgot to call Dispose and / or in general it is incorrectly implemented. But this is a pretty specific case

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions