title: Index was outside the bounds of the array when adding class to list C#
body: <p>Exception: Index was outside the bounds of the array.</p>
  
  <p>First off, I'm familiar with this exception and I have fixed it before but I'm getting this exception at a very strange line in my code. It is being thrown when I'm adding a user created class to a list of classes in my code. I'm completely lost as to why it is throwing this exception and how to fix it.</p>
  
  <pre><code>public static async Task getData()
      {
          // initialize everything
          List&lt;StockData&gt; stockData = new List&lt;StockData&gt;();
          List&lt;StockMarketCompare&gt; stockCompareData = new List&lt;StockMarketCompare&gt;();
          List&lt;StockData&gt; sandpInfo = new List&lt;StockData&gt;();
          List&lt;StockData&gt; sandpDateInfo = new List&lt;StockData&gt;();
          List&lt;StockData&gt; amexList = new List&lt;StockData&gt;();
          List&lt;DateTime&gt; completedDates = new List&lt;DateTime&gt;();
          SymbolInfo symbolClass = new SymbolInfo();
          List&lt;SymbolInfo&gt; ratingSymbols = new List&lt;SymbolInfo&gt;();
          List&lt;StockRating&gt; ratingList = new List&lt;StockRating&gt;();
          bool isGoodToGo = false;
          string symbol, market;
          int activeSymbolsCount = 0;
          int rowCount = 0, completedRowCount = 0;
          DateTime date = new DateTime();
          DateTime searchDate = new DateTime();
  
          using (SqlConnection connection = new SqlConnection("connectionstring"))
          using (SqlCommand sandpCommand = new SqlCommand("select * from dbo.DailyGlobalData where Symbol='" + Calculations.sp500 + "'", connection))
          using (SqlDataAdapter sandpAdapter = new SqlDataAdapter(sandpCommand))
          using (DataTable sandpTable = new DataTable("sandp"))
          using (SqlCommand stockRatingsCommand = new SqlCommand("select * from dbo.StockRatings", connection))
          using (SqlDataAdapter stockRatingsAdapter = new SqlDataAdapter(stockRatingsCommand))
          using (DataTable stockRatingsTable = new DataTable("stockratings"))
          {
              try
              {
                  // fill the sandptable
                  sandpAdapter.Fill(sandpTable);
  
                  if (sandpTable != null)
                  {
                      var sandpQuery = from c in sandpTable.AsEnumerable()
                                       select new StockData { Close = c.Field&lt;decimal&gt;("Close"), Date = c.Field&lt;DateTime&gt;("Date"), High = c.Field&lt;decimal&gt;("High"), Low = c.Field&lt;decimal&gt;("Low"), Volume = c.Field&lt;Int64&gt;("Volume") };
                      sandpInfo = sandpQuery.AsParallel().ToList();
                  }
  
                  // fill the stockratingstable
                  stockRatingsAdapter.Fill(stockRatingsTable);
  
                  if (stockRatingsTable != null)
                  {
                      activeSymbolsCount = stockRatingsTable.Rows.Count;
  
                      var symbolsAmountQuery = from c in stockRatingsTable.AsEnumerable()
                                               select new SymbolInfo { Symbol = c.Field&lt;string&gt;("Symbol"), Market = c.Field&lt;string&gt;("Market") };
                      ratingSymbols = symbolsAmountQuery.AsParallel().ToList();
                  }
  
                  for (int i = 0; i &lt; activeSymbolsCount; i++)
                  {
                      symbol = ratingSymbols.AsParallel().ElementAtOrDefault(i).Symbol;
                      market = ratingSymbols.AsParallel().ElementAtOrDefault(i).Market;
                      ratingList = new List&lt;StockRating&gt;();
  
                      using (SqlCommand historicalRatingsCommand = new SqlCommand("select * from dbo.OldStockRatings where Symbol='" + symbol + "' and Market='" + market + "'", connection))
                      using (SqlDataAdapter historicalRatingsAdapter = new SqlDataAdapter(historicalRatingsCommand))
                      using (DataTable historicalRatingsTable = new DataTable("historicalratings"))
                      {
                          // fill the historical ratings table
                          historicalRatingsAdapter.Fill(historicalRatingsTable);
  
                          if (historicalRatingsTable != null)
                          {
                              completedRowCount = historicalRatingsTable.AsEnumerable().AsParallel().Count();
                              completedDates = historicalRatingsTable.AsEnumerable().AsParallel().Select(d =&gt; d.Field&lt;DateTime&gt;("Date")).ToList();
                          }
                      }
                              using (SqlCommand amexCommand = new SqlCommand("select * from dbo.DailyAmexData where Symbol='" + symbol + "'", connection))
                              using (SqlDataAdapter amexAdapter = new SqlDataAdapter(amexCommand))
                              using (DataTable amexTable = new DataTable("amexdata"))
                              {
                                  // fill the amex data table
                                  amexAdapter.Fill(amexTable);
  
                                  if (amexTable != null)
                                  {
                                      var amexFillQuery = from c in amexTable.AsEnumerable()
                                                          select new StockData { Close = c.Field&lt;decimal&gt;("Close"), Date = c.Field&lt;DateTime&gt;("Date"), High = c.Field&lt;decimal&gt;("High"), Low = c.Field&lt;decimal&gt;("Low"), Volume = c.Field&lt;Int64&gt;("Volume") };
                                      amexList = amexFillQuery.AsParallel().ToList();
  
                                      rowCount = amexList.AsParallel().Count();
                                  }
                              }
  
                      Parallel.For(0, rowCount - 30, new ParallelOptions
                      {
                          MaxDegreeOfParallelism = Environment.ProcessorCount
                      }, async j =&gt;
                      {
                                  if (amexList.AsParallel().Count() &gt; 0)
                                  {
                                      date = amexList.AsParallel().ElementAtOrDefault(j).Date;
                                      searchDate = date.Subtract(TimeSpan.FromDays(60));
  
                                      if (completedDates.Contains(date) == false)
                                      {
                                          var amexQuery = from c in sandpInfo
                                                          where c.Date &gt;= searchDate &amp;&amp; c.Date &lt;= date
                                                          join d in amexList on c.Date equals d.Date
                                                          select new StockMarketCompare { stockClose = d.Close, marketClose = c.Close };
  
                                          var amexStockDataQuery = from c in amexList
                                                                   where c.Date &gt;= searchDate &amp;&amp; c.Date &lt;= date
                                                                   select new StockData { Close = c.Close, High = c.High, Low = c.Low, Volume = c.Volume, Date = c.Date };
  
                                          stockCompareData = amexQuery.AsParallel().ToList();
                                          stockData = amexStockDataQuery.AsParallel().ToList();
                                          isGoodToGo = true;
                                      }
                                      else
                                      {
                                          isGoodToGo = false;
                                      }
                                  }
  
                          if (completedDates.Contains(date) == false)
                          {
                              var sandpDateQuery = from c in sandpInfo
                                                   where c.Date &gt;= searchDate &amp;&amp; c.Date &lt;= date
                                                   select c;
                              sandpDateInfo = sandpDateQuery.AsParallel().ToList();
                              symbolClass = new SymbolInfo() { Symbol = symbol, Market = market };
                              isGoodToGo = true;
                          }
                          else
                          {
                              isGoodToGo = false;
                          }
  
                          if (isGoodToGo)
                          {
                              StockRating rating = performCalculations(symbolClass, date, sandpInfo, stockData, stockCompareData);
  
                                  if (rating != null)
                                  {
                                      **ratingList.Add(rating);** // getting the exception thrown here
                                  }
                          }
                      });
  
                      // now save the results to the table outside the parallel for loop
  
                      ratingList.RemoveAll(item =&gt; item == null);
                      List&lt;StockRating&gt; masterList = ratingList.DistinctBy(j =&gt; j.date).ToList();
                      saveToTable(masterList, symbol, market);
                      // close the connection
                      connection.Close();
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.Message);
              }
              finally
              {
                  // close the connection
                  connection.Close();
              }
          }
      }
  </code></pre>