title: How to avoid changing local variables value to change when not directly accesing it?
body: <p>I have this code that changes the value between 2 positions in a bidimensional array, I have an original bidimensional array called  <code>LocalMatrix</code>, I create a copy of it, and then work with that copy to avoid changing the original values, but everytime I change the value of the copy, it changes the values in the original array, which I never access.</p>
  
  <p>Here's the <code>Matrix</code> class, it has an <code>InnerMatrix</code> which is a regular <code>int[3,3]</code> :</p>
  
  <pre><code>public class Matrix
      {
          public int[,] InnerMatrix { get; set; }        
          public double Fn { get; set; }        
  
          public Matrix()
          {
              InnerMatrix = new int[3,3];
          }
  </code></pre>
  
  <p>And this is the implementation:</p>
  
  <pre><code>public class Solver
      {
          private Matrix LocalMatrix;
  
          public Solver(Matrix matrix)
          {
              LocalMatrix = matrix;
              Explore();            
          }
  
          private void Explore()
          {
              Position zero = FindZero();
              List&lt;Position&gt; possibleMoves = PossibleMoves.GetPossibleMoves(zero);
              PossibleStates(possibleMoves, zero);
          }
  
          private List&lt;Matrix&gt; PossibleStates(List&lt;Position&gt; possibleMoves, Position zero)
          {
              List&lt;Matrix&gt; list = new List&lt;Matrix&gt;();            
              int carry;
  
              possibleMoves.ForEach( position =&gt; 
              {                
                  Matrix matrix = LocalMatrix;
                  carry = LocalMatrix.InnerMatrix[position.i, position.j];
  
  //Here when I change the recently created matrix values, it changes the LocalMatrix values
  
                  matrix.InnerMatrix[zero.i, zero.j] = carry;
                  matrix.InnerMatrix[position.i, position.j] = 0;
  
                  list.Add(matrix);
              });
  
              PrintPossibleStates(list);
  
              return list;
          }
  </code></pre>