title: Cast an object into generic list
body: <p>I have a requirement where I can get the following in an object -</p>
  
  <pre><code>a type T or List&lt;T&gt; 
  </code></pre>
  
  <p>Converting object into T is easy. How can I convert it to List(by first checking that it can be converted successfully or not), reason I want to convert is to scroll through the list and call tostring on each element.</p>
  
  <p>My actual code -</p>
  
  <pre><code>namespace Generic_Collection_Code
  {
      class Program
      {
          public static string DumpObj(object obj)
          {
              string sTemp = String.Empty;
  
              List&lt;int&gt; ints = obj as List&lt;int&gt;;
              if (ints != null)
              {
                  foreach (int i in ints)
                      sTemp += i.ToString() + ",";
                  sTemp.Trim(',');
              }
              else 
              {
                  List&lt;string&gt; strings = obj as List&lt;string&gt;;
                  if (strings != null)
                  {
                      foreach (string s in strings)
                          sTemp += s + ",";
                      sTemp.Trim(',');
                  }
                  else
                  {
                      sTemp += obj.ToString();
                  }
              }
              return sTemp;
          }
          static void Main(string[] args)
          {
              List&lt;int&gt; listInts = new List&lt;int&gt;();
              listInts.Add(1);
              listInts.Add(2);
              listInts.Add(3);
  
              Console.WriteLine("Object1: {0}", DumpObj(listInts));
              int i = 90;
  
              Console.WriteLine("Object2 {0}", DumpObj(i));
  
  
              List&lt;string&gt; listStrings = new List&lt;string&gt;();
              listStrings.Add("1");
              listStrings.Add("2");
              listStrings.Add("3");
  
              Console.WriteLine("Object3: {0}", DumpObj(listStrings));
              Console.ReadKey();
          }
      }
  }
  </code></pre>
  
  <p>The above code works but I know its an ugly way to achieve this. I wanted to ask from community how can I have this function like -</p>
  
  <pre><code>    public static string DumpObj&lt;T&gt;(object obj)
      {
          string sTemp = String.Empty;
  
          List&lt;T&gt; list = obj as List&lt;T&gt;;
          if (list != null)
          {
              foreach (T i in list)
                  sTemp += i.ToString() + ",";
              sTemp.Trim(',');
          }
          return sTemp;
      }
  </code></pre>
  
  <p>This gives me compilation errors as I have to specify T while calling DumpObj with error as -</p>
  
  <p>Error   1   The type arguments for method 'Generic_Collection_Code.Program.DumpObj(object)' cannot be inferred from the usage. Try specifying the type arguments explicitly. D:\DotNet\Generic_Collection_Code\Generic_Collection_Code\Program.cs    57  47  Generic_Collection_Code</p>
  
  <p>as you can see, obj is an object, i dont know its type while calling dumobj.</p>
  
  <p>I hope I have made myself clear on this one.</p>
  
  <p>I appreciate your time!</p>
  
  <p>Regards
  Amit</p>