title: How to instantiate a generic list without knowing the type
body: <p>I created a method to organize a generic list without know the type, it will sort if its int or decimal.</p>
  
  <p>However the code that retrieves the values from textboxes uses List</p>
  
  <p>I tried to convert it to List, but it doesnt work.
  I want this code to work if they type integers or decimals or strings in the textboxes.</p>
  
  <p>This was part of an interview question where they asked not to use the sort method, and that the input should receive for example INTS or DECIMALS</p>
  
  <pre><code>private void btnSort_Click(object sender, EventArgs e)
          {
              List&lt;int&gt; list = new List&lt;int&gt;();
              list.Add(int.Parse(i1.Text));
              list.Add(int.Parse(i2.Text));
              list.Add(int.Parse(i3.Text));
              list.Add(int.Parse(i4.Text));
              list.Add(int.Parse(i5.Text));
              Sort(list);
              StringBuilder sb = new StringBuilder();
              foreach (int t in list)
              {
                  sb.Append(t.ToString());
                  sb.AppendLine();
              }
              result.Text = sb.ToString();
          }
  
  
          private void Sort&lt;T&gt;(List&lt;T&gt; list)
          {
              bool madeChanges;
              int itemCount = list.Count;
              do
              {
                  madeChanges = false;
                  itemCount--;
                  for (int i = 0; i &lt; itemCount; i++)
                  {
                      int result = Comparer&lt;T&gt;.Default.Compare(list[i], list[i + 1]);
  
                      if (result &gt; 0)
                      {
                          Swap(list, i, i + 1);
                          madeChanges = true;
                      }
                  }
              } while (madeChanges);
          }
  
  
          public List&lt;T&gt; Swap&lt;T&gt;(List&lt;T&gt; list,
                  int firstIndex,
                  int secondIndex)
          {
              T temp = list[firstIndex];
              list[firstIndex] = list[secondIndex];
              list[secondIndex] = temp;
  
              return list;
          }
  </code></pre>
  
  <p>I wanted that something like this: but gives error
  Error   1   The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?) c:\users\luis.simbios\documents\visual studio 2010\Projects\InterViewPreparation1\InterViewPreparation1\Generics\GenericsSorting1.cs    22  18  InterViewPreparation1</p>
  
  <p>List list = new List();
          list.Add(i1.Text);
          list.Add(i2.Text);
          Sort(list);</p>