title: C# Generic Method Without Specifying Type
body: <p>Ok so I'm a Java guy starting to use C# and I was coding and started making a generic method and what I wrote runs and compiles but it goes against everything I know about how generics should work so I'm hoping someone can explain this to me:</p>
  
  <p>So I have a generic method defined as follows: </p>
  
  <pre><code>public static List&lt;T&gt; CopyAsList&lt;T&gt;(IEnumerable&lt;T&gt; list, Object lockObject)  
  {  
      if (list != null)  
      {  
          lock (lockObject)  
          {  
              return new List&lt;T&gt;(list);  
          }  
      }  
      return null;  
  }  
  </code></pre>
  
  <p>But the weird thing to me is that I can call this generic method without ever specifying <code>T</code> and it will work:</p>
  
  <pre><code>List&lt;String&gt; strings = new List&lt;string&gt;() { "a", "b", "c"};
  List&lt;int&gt; ints = new List&lt;int&gt;() { 1,2,3};
  object lockObject = new object();
  
  foreach (string s in CopyAsList(strings, lockObject))
  {
      Console.WriteLine(s);
  }
  
  foreach (int i in CopyAsList(ints, lockObject))
  {
      Console.WriteLine(i);
  }
  </code></pre>
  
  <p>How is it the code is able to compile without ever specifying the generic type? Does C# infer the type at runtime?</p>