I have the list below, at the beginning I list the elements of the list {-1, -} by default. 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 what would be the best approach if I need to rewrite 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 better? In the second approach, although I'm sure that two values ββwere initially set, I can risk the exception of Argument index out of range . But the first approach can eat more memory (correct me if I'm wrong!), Because every time I create a new list.
Is there a simpler and / or better solution
topgun_ivard
source share