title: Accessing child properties from a dictionary, which stores parent class type
body: <p>I have a situation where I would like to create a dictionary that can hold parent and child types. I would like to store different child types and be able to call their data. </p>
  
  <p>The problem I am running in to is that when pulling the objects out of the dictionary, they are treated as their parent type, since the dictionary type is set as the parent object. The only way I have figured out to access their additional properties is through instantiating a new object with var (see below).</p>
  
  <p>Any help would greatly be appreciated. Also, if my lingo/terminology is incorrect, please feel free to correct me as I am trying to learn. Thanks!</p>
  
  <p>Update: I have updated my code slightly to try to convey exactly what I am trying to do. (added the foreach loop).</p>
  
  <pre><code>        public class parent
          {
              public string name;     
          }
  
          public class child : parent 
          {
             public int age;      
          }
  
          public class library
          {               
          }
  
          public class Program
          {
             public static void Main(string[] args)
             {                
                Dictionary&lt;int, parent&gt; dictionary = new Dictionary&lt;int, parent&gt;();
                child child1 = new child();
                child child2 = new child();
  
                child1.age = 5;
                child2.age = 10;
  
                dictionary.Add(1,child1);
                dictionary.Add(2,child2);    
  
  
       foreach(KeyValuePair&lt;int, parent&gt; d in dictionary)  
      {
  
      Console.WriteLine(d.age);
  
      }                  
  
  
            }
  }
  </code></pre>