views:

139

answers:

7

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

A: 

Maybe I don't understand your scenario, but I think a better solution would be a simple array??

int[] list = new int[] { -1, 1 };
Jonathan
I personally feel arrays are bad. My aim was to try and avoid them.
topgun_ivard
A: 

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

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

and then to change it:

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

Since the array does not change size, and 2 values are allocated to it, you will not get a IndexOutOfRangeException. I would not usually use the first method to change the contents of a collection - in general it is better to change an existing object than create and new one.


Just as an aside, you can write an initialiser for a List<T> like:

new List<int> {-1, -1};
Callum Rogers
+11  A: 

Or is there a simpler and better solution?

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

Point p = new Point(1, -1);
p = new Point(5, 10);
Console.WriteLine("X = {0}, Y = {1}", p.X, p.Y);
Juliet
Nice call on point. I was thinking IDictionary<int, int> would accomplish the same.
Jarrett Meyer
+1: Using a (dynamically sized) list creates ambiguity for the type - where comments now MUST be relied upon to indicate the intended usage. If a CLR type isn't desired than a custom struct or object is called for.
SnOrfus
+1 - Clever idea!
Jamie Keeling
I think using Point serves the purpose. I don't need to worry about exceptions and the size is FIXED at 2!!Awesome!! thanks Juliet!
topgun_ivard
Nifty, but as the OP isn't actually representing a point in two-dimensional space, the intent is muddled. The approach wouldn't work if the OP had 3 items. Also, the API doesn't indicate that `X` and `Y` are always set at the same time, or that they start at `-1`. See my answer for details.
Bryan Watts
+1  A: 

This looks like it wants to be encapsulated, which would remove the complexity at the usage site.

The encapsulation should provide all behaviors, including starting at -1, -1 and setting both X and Y at the same time. You could 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;
    }
}
Bryan Watts
+1  A: 

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();
    }
}
Jarrett Meyer
Custom classes are good. But I feel it would just be extra code which needs to be maintained. Thanks for the idea though. I love using the getters and setters, makes life manageable :)
topgun_ivard
+1  A: 

Struct could 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);
// ...
p.X = newX;
p.Y = newY;
Mika Kolari
+1  A: 

Approach 2 would be better because Approach 1 causes unnecessary memory allocation (creating the new list, and array, etc.)

However, the fact that your list only ever has 2 items in it makes me think that a list is the wrong class to use in your scenario.

qstarin