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. Lists basics

Lists basics

Scheduled Pinned Locked Moved C#
helpquestiondebugging
3 Posts 3 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.
  • _ Offline
    _ Offline
    _Q12_
    wrote on last edited by
    #1

    im an artist and i code for fun. I just encounter a strange problem in my code and i had to debug it a lot to find this little bug. Not a bug per say, but more like my way of using things (probably wrong). But now is an eye opener.

    //What is the diference between these 2 codes?
    List block = new List();
    List temp = new List();
    void SomeEvent
    {
    //code1
    block.Clear();
    block.AddRange(temp);

    //code2
    block = temp;
    }

    I wanted the text from [temp] list to copy into [block] list. My usual way of doing it, was as in code2. But it seems is a bad way. Because when temp is cleared, also the block is cleared, like block is a pointer to the temp. I only wanted a temporary transfer of data between the 2 lists and not a permanent attachment. Yah... can you explain the real difference? I had to learn it the hard way i suppose. :)

    OriginalGriffO L 2 Replies Last reply
    0
    • _ _Q12_

      im an artist and i code for fun. I just encounter a strange problem in my code and i had to debug it a lot to find this little bug. Not a bug per say, but more like my way of using things (probably wrong). But now is an eye opener.

      //What is the diference between these 2 codes?
      List block = new List();
      List temp = new List();
      void SomeEvent
      {
      //code1
      block.Clear();
      block.AddRange(temp);

      //code2
      block = temp;
      }

      I wanted the text from [temp] list to copy into [block] list. My usual way of doing it, was as in code2. But it seems is a bad way. Because when temp is cleared, also the block is cleared, like block is a pointer to the temp. I only wanted a temporary transfer of data between the 2 lists and not a permanent attachment. Yah... can you explain the real difference? I had to learn it the hard way i suppose. :)

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      When you do this:

      block = temp;

      you don't copy the elements, you copy the reference. In other words, you throw away both the existing value in block, and the actual ilist itself. The AddRange method adds each individual item to the existing collection - if you like, it copies the collection instead of changing the references. Have a look at this: Using struct and class - what's that all about?[^] - it gets a little more advanced than you are asking at the moment, but the early stuff should help.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • _ _Q12_

        im an artist and i code for fun. I just encounter a strange problem in my code and i had to debug it a lot to find this little bug. Not a bug per say, but more like my way of using things (probably wrong). But now is an eye opener.

        //What is the diference between these 2 codes?
        List block = new List();
        List temp = new List();
        void SomeEvent
        {
        //code1
        block.Clear();
        block.AddRange(temp);

        //code2
        block = temp;
        }

        I wanted the text from [temp] list to copy into [block] list. My usual way of doing it, was as in code2. But it seems is a bad way. Because when temp is cleared, also the block is cleared, like block is a pointer to the temp. I only wanted a temporary transfer of data between the 2 lists and not a permanent attachment. Yah... can you explain the real difference? I had to learn it the hard way i suppose. :)

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        The first case deals with items "in the bucket". In the second, you're replacing the original "block bucket", and pointing it to the same bucket that temp is pointing to.

        It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

        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