0

I need to create a dynamically sized list that contains pairs of points for a window's form using C#. The list size will change depending on the data that is loaded. A simplified version of my approach, to simply convey the problem, is as follows (in the real app, the for-loops will iterate over sizes dependent on loaded data):

        int[,] dummyInt = new int[1, 2];
        List<int[,]> test = new List<int[,]>();


        for (int i = 0; i < 100; i++)
        {
            dummyInt[0, 0] = i;
            for (int j = 0; j < 5; j++)
            {
                dummyInt[0, 1] = j;
                test.Add(dummyInt);
            }
        }

        //Show the values in the list for debugging
        foreach (int[,] value in test)
        {
            MessageBox.Show(value.ToString("G"));
        }

Using this approach, all 500 values in the list are [99,4].

What I was expecting/hoping to get was

value 1 [0,0]

value 2 [0,1]

...

value 500 [99,4]

Seems like the list is storing the actual variable, and then changing the value with every iteration of the for loop. How can I store just the value of dummyInt as a new object to the list?

I searched for this, but I'm not sure I know the appropriate vocabulary to nail down the search.

  • You're effectively adding a reference, instantiate a new `int[,]` instead, then add it to the list. – aybe Jan 19 '17 at 20:10
  • "dynamically sized list that contains pairs of points", but instead of this you declare List of 2d arrays. `List> ` is what it looks like it should be – Konstantin Chernov Jan 19 '17 at 20:11
  • There are tons of questions discussing fact that "C# list adds same elements always". I've used one as duplicate that looks close to what you are doing, but feel free to search https://www.bing.com/search?q=c%23%20list%20add%20changes%20last%20object more if you want post to be closed with different duplicate – Alexei Levenkov Jan 19 '17 at 21:17
  • I figured there would be answers out there, but after a fruitless half-hour of searching, I found nothing. I kept searching "c# list add value not reference", or something along those lines, and got nothing. – anarchoNobody Jan 20 '17 at 13:12

2 Answers2

0

Your List object is storing reference to the dummyInt object. If you want to store different values in List you have to create new int array every time you are adding it to List.

Reference: https://msdn.microsoft.com/en-gb/library/4d43ts61(v=vs.90).aspx

Piotshe
  • 59
  • 7
0

Firstly, you don't need a 2-dimensional array if you're just storing a pair of coordinates. The first coordinate can go in the first element of a 1-dimensional array, and the second coordinate can go in the second element of the array. Secondly, the Clone method can be used to make a copy of an array object if you want to force a separate copy of the whole array to exist.

int[] dummyInt = new int[2];
List<int[]> test = new List<int[]>();
for (int i = 0; i < 100; i++)
{
   dummyInt[0] = i;
   for (int j = 0; j < 5; j++)
   {
      dummyInt[1] = j;
      test.Add((int[])dummyInt.Clone());
   }
}

foreach (int[] value in test)
{
   Console.WriteLine("({0},{1})", value[0], value[1]);
}

And finally, an array might not be the best way to store a pair of coordinates. You might want to use a tuple or make your own structure. If you use a Value type (struct) instead of a Reference type (class), you don't need to clone each one.

struct Pair
{
   public int x;
   public int y;
}

public class Test
{
   public static void Main()
   {
      Pair dummyInt = new Pair();
      List<Pair> test = new List<Pair>();
      for (int i = 0; i < 100; i++)
      {
         dummyInt.x = i;
         for (int j = 0; j < 5; j++)
         {
            dummyInt.y = j;
            test.Add(dummyInt);
         }
      }

      foreach (Pair value in test)
      {
         Console.WriteLine("({0},{1})", value.x, value.y);
      }
   }
}

Note how the result is different if you change the word struct at the beginning to class.

BlueMonkMN
  • 25,079
  • 9
  • 80
  • 146
  • Your suggestions are appreciated. I'm very new with using C#. The only kind of programming I've ever really done (if it can be called programming) is using MATLAB, and I used that A LOT...and so, my mind thinks in matrices (2D arrays). The program I have for what I need to do works well in MATLAB, and I'm simply trying to translate it into C#. – anarchoNobody Jan 20 '17 at 13:18