title: Why does the list force all new items to duplicate themselves?
body: <p>I have cut down the code from my project significantly so it's copy and pastable but if you want to debug in a console project it'll need the nuget package: Install-Package MsgPack.Cli.</p>
  
  <p>Ok, below I have commented on the line that is the issue, all I want to know is why the list is forcing duplicates within the _outgoingMessageQueue queue. Is this some kind of captured variable conundrum? Please give as much detail as possible</p>
  
  <pre><code>using MsgPack.Serialization;
  using System;
  using System.Collections.Concurrent;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Text;
  using System.Threading;
  using System.Threading.Tasks;
  using System.Collections;
  
  namespace QueueTest
  {
      public class Message
      {
          public string Data { get; set; }
      }
  
      public class InternalFactoryMsg&lt;T&gt;
      {
          public T Data { get; set; }
          public string Group { get; set; }
          public List&lt;byte[]&gt; ReturnIds { get; set; }
      }
  
      public class FactoryHelpers
      {
          public static List&lt;byte[]&gt; GetReturnIdentities(List&lt;byte[]&gt; messageBytes, byte[] identity)
          {
              var response = new List&lt;byte[]&gt;();
  
              foreach (byte[] part in messageBytes)
              {
                  if (part != null &amp;&amp; part.Length &gt; 0)
                      response.Add(part);
                  else
                      break;
              }
  
              // may not need this with good routing, but can avoid errors
              if (messageBytes.Count &gt; 0 &amp;&amp; messageBytes[0] == identity)
              {
                  messageBytes.RemoveAt(0);
                  Console.WriteLine("[GetReturnIdentities]: Removed identity from start, check your routing!");
              }
  
              // no return identities, send empty list as these bytes will be the 
              // app message and command identity couple
              if (response.Count == messageBytes.Count)
                  return new List&lt;byte[]&gt;();
  
              return response;
          }
  
          public static byte[] SerializeData&lt;T&gt;(T appMsg)
          {
              var serializer = MessagePackSerializer.Get&lt;T&gt;();
  
              using (var byteStream = new MemoryStream())
              {
                  serializer.Pack(byteStream, appMsg);
                  return byteStream.ToArray();
              }
          }
  
          public static T DeserializeData&lt;T&gt;(byte[] bytes)
          {
              try
              {
                  var serializer = MessagePackSerializer.Get&lt;T&gt;();
                  using (var byteStream = new MemoryStream(bytes))
                  {
                      return serializer.Unpack(byteStream);
                  }
              }
              catch (Exception ex)
              {
                  return default(T);
              }
          }
      }
  
      public class Factory: FactoryHelpers
      {
          protected ConcurrentQueue&lt;KeyValuePair&lt;string, List&lt;byte[]&gt;&gt;&gt; _outgoingMessageQueue { get; set; }
          public ConcurrentQueue&lt;KeyValuePair&lt;string, List&lt;byte[]&gt;&gt;&gt; IncomingMessageQueue { get; set; }
  
          public Factory()
          {
              _outgoingMessageQueue = new ConcurrentQueue&lt;KeyValuePair&lt;string, List&lt;byte[]&gt;&gt;&gt;();
              IncomingMessageQueue = new ConcurrentQueue&lt;KeyValuePair&lt;string, List&lt;byte[]&gt;&gt;&gt;();
  
              // add fake incoming message
              var byteMsg = new List&lt;byte[]&gt;()
              {
                  Encoding.Unicode.GetBytes("socket1"),
                  Encoding.Unicode.GetBytes(""),
                  Encoding.Unicode.GetBytes("data")
              };
              var msg = new KeyValuePair&lt;string, List&lt;byte[]&gt;&gt;("socket1", byteMsg);
              IncomingMessageQueue.Enqueue(msg);
          }
  
          public void AddMessage&lt;T&gt;(InternalFactoryMsg&lt;T&gt; msg)
          {
              var msgBytes = msg.ReturnIds ?? new List&lt;byte[]&gt;();
              msgBytes.Add(new byte[0]);
              msgBytes.Add(Factory.SerializeData&lt;T&gt;(msg.Data));
  
              _outgoingMessageQueue.Enqueue(new KeyValuePair&lt;string, List&lt;byte[]&gt;&gt;("socket2", msgBytes));
          }
  
          public List&lt;KeyValuePair&lt;string, List&lt;byte[]&gt;&gt;&gt; GetQueue()
          {
              return _outgoingMessageQueue.ToList();
          }
  
          public static T GetDataFromBytes&lt;T&gt;(List&lt;byte[]&gt; msgBytes)
          {
              // ignoring null checks etc
              return DeserializeData&lt;T&gt;(msgBytes.Last());
          }
      }
  
      public static class MessageLayer
      {
          public static Factory Factory = new Factory();
  
          public static void Init()
          {
              Task.Factory.StartNew(u =&gt;
              {
                  while(true)
                  {
                      KeyValuePair&lt;string, List&lt;byte[]&gt;&gt; msg;
                      if(Factory.IncomingMessageQueue.TryDequeue(out msg))
                      {
                          var data = msg.Value.Last();
                          var returnIds = Factory.GetReturnIdentities(msg.Value, Encoding.Unicode.GetBytes(msg.Key));
                          IncomingCommands.HandleDataCommand(data, "test grp", returnIds);
                      }
                      // nice and slow for simulation
                      Thread.Sleep(400);
                  }
              }, TaskCreationOptions.LongRunning);
          }
  
          public static void SendMessage(Message msg, string group, List&lt;byte[]&gt; returnIds)
          {
              var intMsg = new InternalFactoryMsg&lt;Message&gt;();
              intMsg.Data = msg;
              intMsg.Group = group;
              intMsg.ReturnIds = returnIds;
              Factory.AddMessage&lt;Message&gt;(intMsg);
          }
      }
  
      public static class DataAccessor
      {
          public static List&lt;Message&gt; GetData(byte[] data)
          {
              return new List&lt;Message&gt;() 
              {
                  new Message() { Data = "magic" },
                  new Message() { Data = "data!" }
              };
          }
      }
  
      public static class IncomingCommands
      {
          public static void HandleDataCommand(byte[] data, string group, List&lt;byte[]&gt; returnIds)
          {
              List&lt;Message&gt; result;
              // does big switch, gets data response
              result = DataAccessor.GetData(data);
  
              foreach (Message msg in result)
              {
                  var local = msg;
                  var fix = new List&lt;byte[]&gt;(returnIds);
                  // THIS IS THE ISSUE
                  // comment out the following line and uncomment the one below to fix it
                  // but... why??? :O !!!
                  MessageLayer.SendMessage(local, group, returnIds);
                  //MessageLayer.SendMessage(local, group, fix);
              }
  
              // check the queue
              Console.WriteLine("---------------------------");
              Console.WriteLine("::Checking queue contents::");
              var msgs = MessageLayer.Factory.GetQueue();
  
              foreach(var m in msgs)
              {
                  var check = Factory.GetDataFromBytes&lt;Message&gt;(m.Value);
                  Console.WriteLine("data -&gt; " + check.Data);
              }
          }
      }
  
      public class Program
      {
          static void Main(string[] args)
          {
              MessageLayer.Init();
  
              while(true)
              {
                  Thread.Sleep(400);
              }
          }
      }
  }
  </code></pre>
  
  <p>If you can't work it out, please up vote so it gets attention. Thanks</p>