I think its B
List<string> myData = new List<string>();
myData.Add("string1");
myData.Add("string2");
myData.Add("string3");
while (myData.Count != 0)
{
myData.RemoveAt(0);
}
Console.WriteLine(myData.Count());
The problem with solution A is that it removes on position i.
So you have a list: myData = {"string1", "string2", "string3"}.
The first loop removes the element on position 0. myData is now:
myData = {"string2", "string3"}
The next time it goes through the loop is to remove position 1 in the list. myData is now:
myData = {"string2"}
The next step fails as i now is larger than the size of the list.
Definitely its not C, bcz you can't modify collection through what you iterate in foreach.
See here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/foreach-in
"The foreach statement is used to iterate through the collection to get the information that you want, but can not be used to add or remove items from the source collection to avoid unpredictable side effects. If you need to add or remove items from the source collection, use a for loop."
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
djools
Highly Voted 5 years, 6 months agoXavios
Highly Voted 5 years, 7 months agoM62
Most Recent 3 years, 10 months agonoussa
3 years, 10 months agojohan2020
4 years agoBintang0
4 years, 1 month ago_IngKate
4 years, 2 months agoKilsimon
4 years, 1 month agoJannie
4 years, 5 months agoWTH
4 years, 7 months agoSujay
4 years, 7 months agoArpan3213
4 years, 7 months agoTonyBezerra
4 years, 8 months agoShema
4 years, 10 months agopeter1994
5 years, 3 months agoaadi
5 years, 3 months agosupersunny
5 years, 4 months ago