You have the following code.
You need to remove all of the data from the myData list.
Which code should you use?
A. for (int i = 0; i <= myData.Count; i++) myData.RemoveAt(i);
B. while (myData.Count ! = 0) myData.RemoveAt(0);
C. foreach(string currentString in myData) myData.Remove(currentString);
D. for (int i = 0; i <= myData.Count; i++) myData.RemoveAt(0);
B is the correct answer!!
for (int i = 0; i < myData.Count; i++)
{
myData.RemoveAt(i);
}
The end result will be "One element left out" in the list. Not all will be deleted.
Correct – B
A is correct
in you code you left out i<=MyData.Count that why you getting last element leftout if you added the = opreator you would have deleted all the string in the Array
B – will always remove the element at position 0 only
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.removeat?view=netcore-3.1
“When you call RemoveAt to remove an item, the remaining items in the list are renumbered to replace the removed item. For example, if you remove the item at index 3, the item at index 4 is moved to the 3 position. In addition, the number of items in the list (as represented by the Count property) is reduced by 1.”
So B is the correct answer
Correct Answer is while (myData.Count != 0) myData.RemoveAt(0);
List myData = new List();
myData.Add(“string1”);
myData.Add(“string2”);
myData.Add(“string3”);
Console.WriteLine(myData.Count);
while (myData.Count != 0) myData.RemoveAt(0);
Console.WriteLine(myData.Count);
/*
3
0
*/
Correct.