title: C# Passing struct as parameter
body: <p>I have the following code in C#:</p>
  
  <pre><code>public class ELL
  {
      public struct RVector
      {
          private int ndim;
          private double[] vector;
  
          public RVector(double[] vector) =&gt; (ndim, this.vector) = (vector.Length, vector);
          public double this[int i] { get =&gt; vector[i];  set =&gt; vector[i] = value; }
  
          public override string ToString()
          {
              string str = "(";
  
              for (int i = 0; i &lt; ndim - 1; i++)
                  str += vector[i].ToString() + ", ";
  
              str += vector[ndim - 1].ToString() + ")";
              return str;
          }
      }
      private static void SwapVectorEntries(RVector b, int m, int n)
      {
          double temp = b[m];
          b[m] = b[n];
          b[n] = temp;
      }
      public static void M(string[] args)
      {
          var a = new double[4] { 1, 2, 3, 4 };
          var b = new RVector(a);
  
          Console.WriteLine(b);
          SwapVectorEntries(b, 1, 2); // Why after this command, b will be changed?
          Console.WriteLine(b);
      }
  }
  </code></pre>
  
  <p>In this program, i creates a struct <code>RVector</code>. After that, i use a method <code>SwapVectorEntries</code> which have a struct parameter. Because, Struct is a <code>value type</code>, so i think the method  <code>SwapVectorEntries</code> will not change the struct parameter. But, in the program, after the command <code>SwapVectorEntries(b, 1, 2);</code>, b has changed. Please explain me about this. Thank you !</p>