title: How can I dynamically assign object names in C#?
body: <p>I want to create the objects of class on a mouse click and then save certain values into the values which I want to retrieve afterwards.</p>
  
  <p>Now I am using the dynamic keyword for this, It works fine with the click and insertion but after that I want to retrieve those saved objects and they are more than one so if use the indexing it does not allow me to use that for the object of type dynamic.</p>
  
  <p>My class name is Container which is a user control. And I want to show that in another class on a tablelayoutpanel mouse click</p>
  
  <pre><code>dynamic expando = new Container(); 
  int counter = 0;
  
  private void tableLayoutPanel1_MouseClick(object sender, MouseEventArgs e)
  {
      var pt = new Point(e.X, e.Y);
      var colWidths = this.MatrixPanel.GetColumnWidths();
      var rowHeights = this.MatrixPanel.GetRowHeights();
      int col = -1, row = -1;
      int offset = 0;
  
      for (int iCol = 0; iCol &lt; this.MatrixPanel.ColumnCount; ++iCol)
      {
          if (pt.X &gt;= offset &amp;&amp; pt.X &lt;= (offset + colWidths[iCol]))
          {
              col = iCol;
              break;
          }
          offset += colWidths[iCol];
      }
      offset = 0;
  
      for (int iRow = 0; iRow &lt; this.MatrixPanel.RowCount; ++iRow)
      {
          if (pt.Y &gt;= offset &amp;&amp; pt.Y &lt;= (offset + rowHeights[iRow]))
          {
              row = iRow;
              break;
          }
          offset += rowHeights[iRow];
      }
      expando.Name = "Count" + counter;
      MatrixPanel.Controls.Add(expando, col, row);
  
      expando.setposition(row, col);
      expando.SetChannel(click.ToString());
  
      counter++;
  }
  </code></pre>
  
  <p>Now when I have to retrieve these saved values I have to use the expando list or array How can I declare the List of type Container as well as dynamic?</p>
  
  <pre><code> private void masterTab1_ItemSelected(object sender, object selectedValue)
   {     
      var positions = Adapter.getPositions(RackID);
  
      for (int i = 0; i &lt; positions.Count; i++)
      {
          MatrixPanel.Controls.Add(expando, positions.ElementAt(i).Y, positions.ElementAt(i).X); // Here I want to use expando[i]
  
          expando.setposition(positions.ElementAt(i).X, positions.ElementAt(i).Y); // Here I want to use expando[i]
          expando.SetChannel(positions.ElementAt(i).Channel.ToString()); // Here I want to use expando[i]
      }
   }
  </code></pre>
  
  <p>My question is a bit same like <a href="https://stackoverflow.com/questions/4658726/c-dynamic-object-names">C#, dynamic object names?</a> but if I use the new keyword then how will I be able to call the Container functions as I will have no idea about the name of newly created object?</p>
  
  <p>Can anyone has idea how will I be able to do this?</p>
  
  <p><img src="https://i.stack.imgur.com/88mf5.png" alt="enter image description here"></p>