C# lists... and an error I don't understand.. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

C# lists... and an error I don't understand..

Ok so, i've been learning C# by doing exercises from an Udemy course, in one of these courses we learned about lists. Now I am trying to access an item in this list on position 0 and position 1; (zero-based). Everytime it throws me the following error: System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list. I have no clue how to fix this.. I've tried loops, if statements , iterations, ... if try everything in my arsenal and I am stuck. Code here -> https://code.sololearn.com/c8a42DkJbfd4/#cs

25th Apr 2020, 10:30 PM
BullseyeBE
BullseyeBE - avatar
2 Answers
+ 3
Your issue is with your Console.WriteLine code. The format specifiers are zero based, but you are starting with 1. For example: Console.WriteLine("{1} has liked your post", names[0]); Should be: Console.WriteLine("{0} has liked your post", names[0]);
25th Apr 2020, 11:24 PM
Chris Persichetti
Chris Persichetti - avatar
+ 3
The range used in for-loop at line 42 is out. Use for (int i = 0; i < names.Count; i++). Actually, still on the same `if` block, no loop is needed if there's only 1 person who voted. When there's more than 2 voters, show name of 1st and 2nd voter first, then subtract total voters by 2, that is the remaining number of voter you show after the first 2 voters names. (Edit) On second thought, you don't need loop in any case here. You can show one or two voter name, and calculate the remaining count of voter when there was more than 2 voters in the list.
26th Apr 2020, 4:18 AM
Ipang