title: Retrieving field data from a List of class objects
body: <p>I am ultimately trying to store Vertex data: x, y, z. I am comparing two different collection types to store the data: <code>List&lt;T&gt;</code> and <code>Dictionary&lt;K,V&gt;</code>. Both contain the same public class data. Note the differences; my list uses a constructor (I add to the list later) and my dict uses a method to ADD to the dict: </p>
  
  <p>my LIST</p>
  
  <pre><code>class Node1: List&lt;Node1&gt;
  {
      public int nid;
      public double x, y, z;
      public Node1() {}
      public Node1(string strLine)
      {
          nid = int.Parse(strLine.Substring(8, 8));
          x = double.Parse(strLine.Substring(24, 8));
          y = double.Parse(strLine.Substring(32, 8));
          z = double.Parse(strLine.Substring(40, 8));
      }
  }
  </code></pre>
  
  <p>and there's this one... DICTIONARY</p>
  
  <pre><code>public struct loc
  {
      public double x, y, z;
  }
  public class Node2: Dictionary&lt;int,loc&gt;
  {
      public int nid;
      public void Add(string strLine)
      {
          // collect the dictionary KEY
          nid = int.Parse(strLine.Substring(8, 8));
          // collect the dictionary VALUE
          loc data;
          data.x = double.Parse(strLine.Substring(24, 8));
          data.y = double.Parse(strLine.Substring(32, 8));
          data.z = double.Parse(strLine.Substring(40, 8));
          this.Add(nid, data);
      }
  }
  </code></pre>
  
  <p>In my main I read in a text file with the Vertex data and do the following (a lot has been removed):</p>
  
  <pre><code>var Nodes1 = new Node1();
  var Nodes2 = new Node2();
  
  string strLine;
  
  Nodes1.Add(new Node1(strLine));
  Nodes2.Add(strLine);
  </code></pre>
  
  <p>Both collections now contain the same data.</p>
  
  <p>Now here comes the question. I am looking for a specific instance of the class with the int "nid" that equals 1001 and I want to find the value of the corresponding double "x". To clarify that somewhat (because I just confused myself), I can check to see if an instance that contains nid=1001 exists by doing the following:</p>
  
  <pre><code>// does the entry exist - LIST
  bool here1 = Nodes1.Exists(var =&gt; var.nid == 1001);
  
  // does the entry exist - DICTIONARY
  bool here2 = Nodes2.ContainsKey(1001);
  </code></pre>
  
  <p>To get the double "x" from the object that contains nid=1001 (for the dict) I do:</p>
  
  <pre><code>double here3 = Nodes2[1001].x;
  </code></pre>
  
  <p>But I cannot figure out how to get the data from the List collection. Perhaps it is because the List collection was not intended for this purpose. But, if possible, how do I get the same out of the List?</p>
  
  <p>Can someone also, perhaps, explain a another (better) way to do this? I still don't have a great understanding of WHY/WHEN to use the LIST and DICTIONARY collections as there seems to be 100 ways to do the same thing.</p>