title: Double declaration and initialization of vector in a class C#
body: <p>How to make this vector double declaration correctly?</p>
  <pre><code>var asset = new Vector&lt;double&gt;(new double[2 * N + 1], -N)
           
  asset[-N] = S * Math.Exp(-N * dx);
  </code></pre>
  <p>In the definition: <strong>asset[-N] = S * Math.Exp(-N * dx);</strong> is showing the error</p>
  <p><strong>The property or indexer &quot;Vectorthis.[int]&quot; cannot be assigned as it is read-only.</strong></p>
  <p>Compiler error CS0200 - Property or indexer 'property' cannot be assigned to -- it is read only</p>
  <p><strong>full code below:</strong></p>
  <pre><code>using System;
  using System.Numerics;
  
  namespace TrinomialTree
  {
      public class TrinomialTree
      {
          TrinomialParameters par;
          public TrinomialTree(TrinomialParameters pars)
          {
              this.par = pars;
              // Your program starts here:
              Console.WriteLine(&quot;Hello world!&quot;);
  
              // Declare and initialize the parameters
              TrinomialParameters myData;
              // Clewlow p. 55 C = 8.42534 for N = 3
              myData.sigma = 0.2;
              myData.T = 1.0; // One year
              myData.r = 0.06;
              myData.K = 100;
              myData.div = 0.0;
              myData.type = 'C';
              myData.exercise = false;
              myData.NumberOfSteps = 3;
              Console.WriteLine(&quot;How many timesteps: &quot;);
              myData.NumberOfSteps = Convert.ToInt32(Console.ReadLine());
              // Now define option-related calculations and price
              TrinomialTree myTree = new TrinomialTree(myData);
              Console.WriteLine(&quot;Price {0}&quot;, myTree.Price(100));
          }
  
          double Price(int strike)
          {
              int N = 2;
              int S = strike;
  
              Vector&lt;Vector&lt;double&gt;&gt; option;
  
              double sig2 = par.sigma * par.sigma;
              double dt = par.T / N;
              double nu = par.r - par.div - 0.5 * sig2;
              // Since trinomial is explicit FDM we have a constraint between
              // dt and dx (Clewlow inequality (3.27))
              double dx = par.sigma * Math.Sqrt(3.0 * dt) + dt * 1;
              double edx = Math.Exp(dx);
              // Initialize asset prices at *maturity*.
              // Notice that the start index is a negative number
              
              var asset = new Vector&lt;double&gt;(new double[2 * N + 1], -N);
  
              //Vector&lt;double&gt; asset = new Vector&lt;double&gt;(2 * N + 1, -N);
  
              //var x = new Vector&lt;double&gt;(new double[2 * N + 1], -N);
  
              asset[-N] = S * Math.Exp(-N * dx);
  
              for (int n = -N + 1; n &lt;= N; n++)
              {
                  asset[n] = asset[n - 1] * edx;
              }
              // Initialize option values at maturity.
              int minIndex = 0;
              option = new Vector&lt;Vector&lt;double&gt;&gt;(N + 1, minIndex);
  
              for (int n = N; n &gt;= 0; n--)
              {
                  for (int j = -n; j &lt;= n; j++)
                  {
                      option[n] = new Vector&lt;double&gt;(2 * n + 1, -n);
                  }
              }
              int nn = N;
              if (par.type == 'C')
              {
                  for (int j = -nn; j &lt;= nn; j++)
                  {
                      option[nn][j] = Math.Max(0.0, asset[j] - par.K);
                  }
  
              }
  
              // Step back through lattice, start from maturity as given value at n = N.
              // European option
              if (par.exercise == false)
              {
                  for (int n = N - 1; n &gt;= 0; n--)
                  {
                      for (int j = -n; j &lt;= n; j++)
                      { // eq. 10.6
                          option[n][j] = disc * (pu * option[n + 1][j + 1] + pm * option[n + 1][j]
                          + pd * option[n + 1][j - 1]);
                      }
                  }
              }
              else // American put only.
              {
                  double tmp;
                  for (int n = N - 1; n &gt;= 0; n--)
                  {
                      for (int j = -n; j &lt;= n; j++)
                      { // eq. 10.6
                          tmp = disc * (pu * option[n + 1][j + 1] + pm * option[n + 1][j]
                          + pd * option[n + 1][j - 1]);
                          // American correction term
                          option[n][j] = Math.Max(tmp, par.K - asset[j]);
                      }
                  }
              }
  
              return option[0][0];
          }
  
          public struct TrinomialParameters
          {
              // Option data
              public double sigma;
              public double T;
              public double r;
              public double K;
              public double div; // Dividend
              public char type; // 'C' or 'P'
              public bool exercise; // false if European, true if American
                                    // 'Numeric' data
              public int NumberOfSteps; // Nr. of subdivisions of [0, T]
          }
      }
  
  }
  </code></pre>