Best .NET List Approach

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

+7
collections list c #
source share
7 answers

Or is there a simpler and better solution?

Yes. Since the list has a fixed size, use a real object like System.Drawing.Point :

 Point p = new Point(1, -1); p = new Point(5, 10); Console.WriteLine("X = {0}, Y = {1}", pX, pY); 
+12
source share

It looks like he wants to be encapsulated in order to eliminate the complexity of the use site.

Encapsulation should provide all kinds of behavior, including starting with -1, -1 and at the same time set both X and Y You can do something like this:

 public class ItemSet { public ItemSet() { this.X = -1; this.Y = -1; } public int X { get; private set; } public int Y { get; private set; } public void SetItems(int x, int y) { this.X = x; this.Y = y; } } 
+1
source share

why not a custom class, something like, especially since it is a fixed size.

 class MyClass { public MyClass(int x, int y) { } public int X { get; set; } public int Y { get; set; } public int[] ToArray() { return new[] { X, Y }; } public List<int> ToList() { return ToArray().ToList(); } } 
+1
source share

String can work too

 public struct Point { public int X { get; set; } public int Y { get; set; } public Point(int x, int y):this() { this.X = x; this.Y = y; } } Point p = new Point(-1, -1); // ... pX = newX; pY = newY; 
+1
source share

Approach 2 will be better because approach 1 causes unnecessary memory allocation (creating a new list, array, etc.)

However, the fact that there are only two elements in your list makes me think that the list is the wrong class to use in your script.

+1
source share

I may not understand your scenario, but I think a simple array would be the best solution.

 int[] list = new int[] { -1, 1 }; 
0
source share

I would recommend that you use an array, which means that the collection remains a fixed size, and the second to access it. So:

 int[] array = new[] { -1, -1 }; 

and then change it:

 array[0] = x; array[1] = y; 

Since the array does not resize and is assigned 2 values, you will not get an IndexOutOfRangeException . Usually I did not use the first method to change the contents of the collection - in general, it is better to modify an existing object than create a new one.


As an aside, you can write an initializer for List<T> as:

 new List<int> {-1, -1}; 
0
source share

All Articles