topgun_ivard
topgun_ivard

Reputation: 8564

.NET List best approach

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

Upvotes: 7

Views: 359

Answers (7)

Bryan Watts
Bryan Watts

Reputation: 45445

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;
    }
}

Upvotes: 1

quentin-starin
quentin-starin

Reputation: 26628

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.

Upvotes: 1

Mike Koder
Mike Koder

Reputation: 1928

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;

Upvotes: 1

Jarrett Meyer
Jarrett Meyer

Reputation: 19573

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();
    }
}

Upvotes: 1

Juliet
Juliet

Reputation: 81516

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);

Upvotes: 12

Callum Rogers
Callum Rogers

Reputation: 15819

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};

Upvotes: 0

Jonathan
Jonathan

Reputation: 1507

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

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

Upvotes: 0

Related Questions