Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Strange List<string[]> behaviour...

Strange List<string[]> behaviour...

Scheduled Pinned Locked Moved C#
algorithmsdata-structuresquestionlearning
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    OlofBakker
    wrote on last edited by
    #1

    Good morning, I have been searching for a long time, until I nailed this behaviour:

    public static void Test()
    {
    string[] strArray = { "", ""};
    List strArrayList = new List();

            strArray\[0\] = "A";
            strArrayList.Add(strArray);
            strArray\[0\] = "B";
            strArrayList.Add(strArray);
            strArray\[0\] = "C";
            strArrayList.Add(strArray);
            MessageBox.Show("strArrayList: " + strArrayList\[0\]\[0\] + strArrayList\[1\]\[0\] + strArrayList\[2\]\[0\]);
            // I expect: "ABC", but it shows: "CCC"!
    
            string str = "";
            List strList = new List();
            str = "A";
            strList.Add(str);
            str = "B";
            strList.Add(str);
            str = "C";
            strList.Add(str);
            MessageBox.Show("strList: " + strList\[0\] + strList\[1\] + strList\[2\]);
            // I expect: "ABC", and it shows: "ABC"!
        }
    

    When I change a string and then add it to a list, everything is fine. However, when I change a string in a string array and then add this array to a list, all previously stored arrays in the list are updated as well! Is this normal or am I doing something wrong? Of course, there is this ridiculous work-around "strArrayList.Add(strArray.ToList().ToArray());" Which works! :-D Thanks in advance, Olof

    I 1 Reply Last reply
    0
    • O OlofBakker

      Good morning, I have been searching for a long time, until I nailed this behaviour:

      public static void Test()
      {
      string[] strArray = { "", ""};
      List strArrayList = new List();

              strArray\[0\] = "A";
              strArrayList.Add(strArray);
              strArray\[0\] = "B";
              strArrayList.Add(strArray);
              strArray\[0\] = "C";
              strArrayList.Add(strArray);
              MessageBox.Show("strArrayList: " + strArrayList\[0\]\[0\] + strArrayList\[1\]\[0\] + strArrayList\[2\]\[0\]);
              // I expect: "ABC", but it shows: "CCC"!
      
              string str = "";
              List strList = new List();
              str = "A";
              strList.Add(str);
              str = "B";
              strList.Add(str);
              str = "C";
              strList.Add(str);
              MessageBox.Show("strList: " + strList\[0\] + strList\[1\] + strList\[2\]);
              // I expect: "ABC", and it shows: "ABC"!
          }
      

      When I change a string and then add it to a list, everything is fine. However, when I change a string in a string array and then add this array to a list, all previously stored arrays in the list are updated as well! Is this normal or am I doing something wrong? Of course, there is this ridiculous work-around "strArrayList.Add(strArray.ToList().ToArray());" Which works! :-D Thanks in advance, Olof

      I Offline
      I Offline
      ignrod
      wrote on last edited by
      #2

      You have to take into account that strArray is a reference type. It seems that when you add it to strArrayList, a reference is added. Then, strArrayList contains a list of references to string arrays, which are the same array since you have been reusing the variable without reasignating it. This is similar to what happens in the following case:

      public void Test() {
      string[] a = new string[] {"", ""};
      string[] b = a;
      a[0] = "A";
      b[0] = "B";
      Console.WriteLine("{0} {1}", a[0], b[0]);
      }

      O 1 Reply Last reply
      0
      • I ignrod

        You have to take into account that strArray is a reference type. It seems that when you add it to strArrayList, a reference is added. Then, strArrayList contains a list of references to string arrays, which are the same array since you have been reusing the variable without reasignating it. This is similar to what happens in the following case:

        public void Test() {
        string[] a = new string[] {"", ""};
        string[] b = a;
        a[0] = "A";
        b[0] = "B";
        Console.WriteLine("{0} {1}", a[0], b[0]);
        }

        O Offline
        O Offline
        OlofBakker
        wrote on last edited by
        #3

        Thank you, Olof

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups