title: How to add elements in custom Dictionary using C#?
body: <p>I have a dictionary for clustering purpose in a class named <code>Cluster</code> in C# as:  </p>
  
  <p><code>Dictionary&lt;int, List&lt;ClusterMember&gt;&gt;</code>  </p>
  
  <p>whereas <code>int</code> represent the <em>cluster id</em>, <code>List&lt;ClusterMember&gt;&gt;</code> represent the <em>members</em> in that <em>cluster id</em> and <code>ClusterMember</code> is another class. I have shown here the whole code structure as:  </p>
  
  <pre><code>public class ClusterMember
  {
      public string _name { get; set; }
  }
  
  public class Cluster
  {
      public Dictionary&lt;int, List&lt;ClusterMember&gt;&gt; _dic { get; set; }  
  
      public Cluster(int _id, List&lt;ClusterMember&gt; _clusMem)
      {
         _dic.Add(_id, _clusMem);
      }
  }  
  </code></pre>
  
  <p>whereas I have used these classes in this method as:  </p>
  
  <pre><code>public static List&lt;Cluster&gt; DP_Cluster(List&lt;string&gt; _customer, double _alpha)
  {
      var _currentClusters = 0; // current number of clusters i.e. "k"
      var _memberNumber = 0;    // running member number i.e. "n"
      //var _dic = new Dictionary&lt;int, List&lt;string&gt;&gt;();
      var _probOld = 0.0;
      var _probNew = 0.0;
  
  
      List&lt;Cluster&gt; _myClusters = new List&lt;Cluster&gt;();
      //Cluster _cluster = new Cluster(?);  
      // How to Add cluster using above classes
      // How to Add cluster Member using above classes  
      _myClusters.Add(_cluster);
  
      //_dic.Add(_currentClusters, _customer.ElementAt(_memberNumber));
      _currentClusters += 1;
  
      for(int _i = 1; _i &lt; _customer.Count - 1; _i++)
      {
          if( _i &lt;= _currentClusters)
          {
              _probOld = myClusters[_i].Members.Count / ((_i+1) - 1 + _alpha);
          }
          else
          {
              _probNew = _alpha / ((_i+1) - 1 + _alpha);
          }
  
          if(_probNew &gt; _probOld)
          {
              // Add _customer.ElementAt(_memberNumber+=1) to New Cluster
              Cluster cluster = new Cluster( _currentClusters += 1 ); 
              myClusters.Add(cluster);
          }
          else
          {
              // Add _customer.ElementAt(_memberNumber+=1) to Old Cluster
          }
      }
  
      return myClusters;
  }  
  </code></pre>
  
  <p>Now how do I Add in <code>_dic</code> object of <code>Dictionary</code> while in <code>for loop</code>? Moreover I may have to add more than one <em>cluster members</em> to same <code>_id</code> more than once as   </p>
  
  <ul>
  <li>I may add a <em>cluster member</em> to <code>_id = 1</code> in an iteration,</li>
  <li>then have to add a <em>cluster member</em> to an <code>_id = 2</code> in another
  iteration,</li>
  <li>then it may turn to add another <em>cluster member</em> to same id i.e. <code>_id = 1</code> and vice versa.  </li>
  </ul>
  
  <p>Moreover, it'll be more meaningful to me if it is possible to start <em>cluster id</em> with <code>1</code> (not <code>0</code>as of default <code>_dic</code> <em>index</em>).</p>