I have a list which is declared below, at the start I default the list items to { -1, - }. please note that throughout the program the list size is fixed at 2.
List<int> list = new List<int>(new int[] {-1, -1});
My question is regarding, what would be the best approach if I need to overwrite the two values in the list.
int x = GetXValue();
int y = GetYValue();
Approach 1:
list = new List<int>(new int[] {x, y});
Approach 2:
list[0] = x;
list[1] = y;
What would be a better approach? With the second approach, even though I am sure there are 2 values set initially, I can run a risk of the Argument index out of range
exception. But the first approach might eat more memory (correct me if I am wrong!) since I am creating a new list every time.
Is there a simpler, and/or better solution