List<int> MyLottoNumbers = new List<int>();
MyLottoNumbers[0] = int.Parse(textBoxNum1.Text);
textBoxNum1 has a value of 5
This code gives the error
Index was out of range. Must be non-negative and less than the size of the collection.
Why ?
It's because your list is currently empty, so you can't set the first index to something (because it doesn't exist). If you did this:
List<int> MyLottoNumbers = new List<int>();
MyLottoNumbers.Add(int.Parse("5"));
MyLottoNumbers[0] = int.Parse("7");
it works, because that index has been set.
If you want to insert at the front, take this route:
List<int> MyLottoNumbers = new List<int>();
MyLottoNumbers.Insert(0, int.Parse(textBoxNum1.Text));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With