title: What is the practical difference between dynamic and T in C#
body: <p>I read about Type <code>dynamic</code> in C# 2010. (the corresponding <a href="http://msdn.microsoft.com/en-us/library/vstudio/dd264736.aspx" rel="noreferrer">msdn</a> entry)</p>
  
  <p>I am confused about the practical difference between <code>T</code> and <code>dynamic</code> while developing generic functions. My current tests didn't show new ways to use <code>dynamic</code>a way, that isn't possible with <code>T</code>. In addition it seems, that dynamic needs much more time in runtime to achieve the same tasks.</p>
  
  <p>Some example code to make my point clear:</p>
  
  <pre><code>// Sample Object
  public class SampleObj
  {
      public string Test { get; set; }
  }
  </code></pre>
  
  <p>My Test-Method (<em>measuring speed as well</em>):</p>
  
  <pre><code>static void Main(string[] args)
  {
      var sampleObj1 = new SampleObj { Test = "Test1" };
      var sampleObj2 = new SampleObj { Test = "Test2" };
  
      Stopwatch c1 = Stopwatch.StartNew();
      bool res1 = CompareObjectsT&lt;SampleObj&gt;(sampleObj1, sampleObj2);
      c1.Stop();
      Console.WriteLine("Comparison T: {0} time: {1} ms", res1, c1.ElapsedMilliseconds);
  
  
      Stopwatch c2 = Stopwatch.StartNew();
      bool res2 = CompareObjectsDyna(sampleObj1, sampleObj2);
      Console.WriteLine("Comparison dynamic: {0} time: {1} ms", res2, c2.ElapsedMilliseconds);
      c2.Stop();
  
      var instance = new X&lt;SampleObj&gt;();
  
      Console.ReadLine();
  }
  </code></pre>
  
  <p>The result is:</p>
  
  <pre><code>Comparison T: False time: 6 ms
  Comparison dynamic: False time: 3969 ms
  </code></pre>
  
  <p>The dynamic functions needs <strong>much</strong> more time, compared to the T comparison.</p>
  
  <p>Decompiling my test application reveals heavy usage of reflection which may lead to this huge amount of time:</p>
  
  <pre><code>private static bool CompareObjectsDyna([Dynamic] object source1, [Dynamic] object source2)
  {
      if (&lt;CompareObjectsDyna&gt;o__SiteContainer2.&lt;&gt;p__Site3 == null)
      {
          &lt;CompareObjectsDyna&gt;o__SiteContainer2.&lt;&gt;p__Site3 = CallSite&lt;Func&lt;CallSite, object, bool&gt;&gt;.Create(Binder.Convert(CSharpBinderFlags.None, typeof(bool), typeof(Program)));
      }
      if (&lt;CompareObjectsDyna&gt;o__SiteContainer2.&lt;&gt;p__Site4 == null)
      {
          &lt;CompareObjectsDyna&gt;o__SiteContainer2.&lt;&gt;p__Site4 = CallSite&lt;Func&lt;CallSite, Type, object, object, object&gt;&gt;.Create(Binder.InvokeMember(CSharpBinderFlags.None, "Equals", null, typeof(Program), new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.IsStaticType | CSharpArgumentInfoFlags.UseCompileTimeType, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }));
      }
      return &lt;CompareObjectsDyna&gt;o__SiteContainer2.&lt;&gt;p__Site3.Target(&lt;CompareObjectsDyna&gt;o__SiteContainer2.&lt;&gt;p__Site3, &lt;CompareObjectsDyna&gt;o__SiteContainer2.&lt;&gt;p__Site4.Target(&lt;CompareObjectsDyna&gt;o__SiteContainer2.&lt;&gt;p__Site4, typeof(object), source1, source2));
  }
  </code></pre>
  
  <p>I considered <a href="https://stackoverflow.com/questions/961581/whats-the-difference-between-dynamicc-4-and-var">this post</a> but this hasn't ansered my question. </p>
  
  <p>Can someone tell me, in what scenario dynamic is much more effective than T?
  (<em>hopefully with a small practical sample</em>)</p>