title: C# static function is sending wrong arguments to function
body: <p>My code is passing the wrong argument to a function for some reason.</p>
  <p>I have a <code>static</code> class say class <code>A</code> having this function <code>AddMaster</code> :</p>
  <pre class="lang-cs prettyprint-override"><code>public static void AddMaster(string ipAddress, int port, List&lt;RegisterMap&gt; registers)
  {
  
      // THIS LINE PRINTS THE ACTUAL VALUES SENT FROM THE CALLER FUNCTION
      System.IO.File.AppendAllText(&quot;datalog_MB.txt&quot;, ipAddress + &quot;   &quot; + registers[0].FriendlyName + &quot;\n&quot;);
      
      new Thread(() =&gt;
      {
          _tasks.Add(Task.Factory.StartNew(() =&gt;
          {
              Monitor.Enter(_masters);
              _masters.Add(new Master().Connect(ipAddress, port).SetRegisters(registers));
  
              _masters.Last().OnEvent += MasterEvent;
  
              Debug.WriteLine(_masters.Count + &quot; TCP masters connected&quot;);
              Monitor.Exit(_masters);
          }));
  
      }).Start();
  
  }
  </code></pre>
  <p>I have another <code>non-static</code> class <code>Master</code> having the function <code>SetRegisters</code>:</p>
  <pre class="lang-cs prettyprint-override"><code>public Master SetRegisters(List&lt;RegisterMap&gt; registerList)
  {
      // HERE THE FriendlyName ALWAYS PRINTS THE VALUE OF THE LAST FUNCTION CALL
      System.IO.File.AppendAllText(&quot;datalog_MB_1.txt&quot;, _hostname + &quot;   &quot; + registerList[0].FriendlyName + &quot;\n&quot;);
      _registersToRead = registerList;
      return this;
  }
  </code></pre>
  <p>The function <code>AddMaster()</code> is called in a loop.</p>
  <p>The first code logs the following which is correct:</p>
  <pre><code># datalog_MB.txt
  192.168.0.12   192.168.0.12:Value 1
  192.168.0.11   192.168.0.11:Value 1
  </code></pre>
  <p>However the second code block prints the following ( See the second value has changed ):</p>
  <pre><code># datalog_MB_1.txt
  192.168.0.12   192.168.0.11:Value 1
  192.168.0.11   192.168.0.11:Value 1
  </code></pre>
  <p><strong>Edit #1</strong></p>
  <pre class="lang-cs prettyprint-override"><code>foreach (var equipment in MSSQL.GetEquipments(true)) 
  {
      registers.Clear();
      
      System.IO.File.AppendAllText(&quot;dataeq.txt&quot;, equipment.IPAddress + &quot;    &quot; + equipment.Name + &quot;     &quot; + equipment.ID +  &quot;\n&quot;);
      
      try
      {
          registers.Add(
              new RegisterMap
              {
                  FriendlyName = equipment.IPAddress + &quot;:Value 1&quot;,
                  Register = 2001,
                  Type = RegisterType.HoldingRegister,
                  StationID = 1
              });
          
          registers.Add(
              new RegisterMap
              {
                  FriendlyName = equipment.IPAddress + &quot;:Value 2&quot;,
                  Register = 2002,
                  Type = RegisterType.HoldingRegister,
                  StationID = 1
              });
  
          A.AddMaster(equipment.IPAddress, 502, registers);
  
          var json = new JavaScriptSerializer().Serialize(registers);
          
          System.IO.File.AppendAllText(&quot;data_reg.txt&quot;, json + &quot;\n\n&quot;);
      }
      catch(Exception err)
      {
          System.Windows.MessageBox.Show(err.Message);
      }
  }
  </code></pre>
  <p><em>Edit #2</em>*</p>
  <p>Fiddle: <a href="https://dotnetfiddle.net/h3yn7p" rel="nofollow noreferrer">https://dotnetfiddle.net/h3yn7p</a></p>
  <p>Any idea what might be going wrong?</p>