title: Writing a function using generics
body: <p>I'm trying to write a C# function and make it accept any type parameter. I want to do it with generic list, but for some reason I can't get it working. How to do it? Are there other ways?</p>
  
  <pre><code>public class City
  {
      public int Id;
      public int? ParentId;
      public string CityName;
  }
  
  public class ProductCategory
  {
      public int Id;
      public int? ParentId;
      public string Category;
      public int Price;
  }
  
  public class Test
  {
  
          public void ReSortList&lt;T&gt;(IEnumerable&lt;T&gt; sources, ref IEnumerable&lt;T&gt; returns, int parentId)
          {
             //how to do like this:
             /*
              var parents = from source in sources where source.ParentId == parentId &amp;&amp; source.ParentId.HasValue select source;
  
              foreach (T child in parents)
              {
                  returns.Add(child);
                  ReSortList(sources, ref returns, child.Id);
              }
             */
          }
  
          public void Test()
          {
              IList&lt;City&gt; city = new List&lt;City&gt;();
  
              city.Add(new City() { Id = 1, ParentId = 0, CityName = "China" });
              city.Add(new City() { Id = 2, ParentId = null, CityName = "America" });
              city.Add(new City() { Id = 3, ParentId = 1, CityName = "Guangdong" });
  
              IList&lt;City&gt; results = new List&lt;City&gt;();
              ReSortList&lt;City&gt;(city, ref results, 0);  //error
          }
  }
  </code></pre>