title: Generics and collections ... struggling with an implementation
body: <p>I am trying to figure out a way to leverage generics so I can make the property Value be an actual type that initialized (not sure if this is the correct way of saying it) when my collection class is created.</p>
  
  <p>I would like to have the syntax be something like:</p>
  
  <pre><code>var list = new ListItemCollection&lt;Guid&gt;(parameters would go here);
  </code></pre>
  
  <p>I have the following class:</p>
  
  <pre><code>[Serializable]
  public class ListItem
  {
      public object Value { get; set; }
      public string Text { get; set; }
      public object DataContext { get; set; }
      public Nullable&lt;bool&gt; Checked { get; set; }
  
      public ListItem()
      {
          this.Checked = false;
      }
  }
  </code></pre>
  
  <p>I have the following collection:</p>
  
  <pre><code>[Serializable]
  public class ListItemCollection : List&lt;ListItem&gt;
  {
      public ListItem this[object value]
      {
          get
          {
              foreach (var child in this)
              {
                  if (child.Value.Equals(value))
                      return child;
              }
              return null;
          }
      }
  
      public bool Contains(object value)
      {
          foreach (var child in this)
          {
              if (child.Value.Equals(value))
                  return true;
          }
          return false;
      }
  
      public void Add(object value, string text)
      {
          this.Add(value, text, null);
      }
  
      public void Add(object value, string text, object dataContext)
      {
          var child = new ListItem();
          child.Value = value;
          child.Text = text;
          child.DataContext = dataContext;
          this.Add(child);
      }
  
      public ListItemCollection()
      {
      }
  
      public ListItemCollection(IEnumerable items,
          string displayMember,
          string valueMember,
          bool showEmptyItem,
          string emptyItemText,
          object emptyItemValue)
      {
          if (showEmptyItem)
          {
              this.Add(emptyItemValue, emptyItemText);
          }
  
          foreach (object item in items)
          {
              object text = null;
              object value = null;
  
              text = item.GetType().GetProperty(displayMember).GetValue(item, null);
              value = item.GetType().GetProperty(valueMember).GetValue(item, null);
  
              // Add the item
              this.Add(value, text.ToString(), item);
          }
      }
  }
  </code></pre>