title: Does compiler optimizes unnecessary/redundant upcast away or Does it produce any IL at all?
body: <p>From <a href="https://stackoverflow.com/questions/5278674/do-redundant-casts-get-optimized">Do redundant casts get optimized?</a> I can see compiler doesn't optimizes unnecessary downcast (i.e. <code>castclass</code>) away. But now I am interested in a simpler case, " if compiler optimizes unnecessary upcast away?" This question only concerns reference type, not <code>boxing</code>.</p>
  
  <p>It seems to my that <code>upcast</code> doesn't produce any IL, and hence redundant explicit <code>upcast</code> doesn't cost at all? Or because IL instruction is typeless, there is still performance cost for redundant explicit <code>upcast</code> behind the scene?</p>
  
  <p>Or would upcast produce any IL instructions sometimes?</p>
  
  <pre><code>class Foo
  {
  }
  class Bar : Foo
  {
  }
  bool Test(object x)
  {
      return x == null;
  }
  void Main()
  {
      var x = new Bar();
      Console.Write(Test((Foo)x)); // redundant explicit to Foo, and then implicit to Object
      var y = new Bar();           // implicit to Object
      Console.Write(Test(y));
  }
  
  IL_0000:  newobj      UserQuery+Bar..ctor
  IL_0005:  stloc.0     // x
  IL_0006:  ldarg.0     
  IL_0007:  ldloc.0     // x
  IL_0008:  call        UserQuery.Test
  IL_000D:  call        System.Console.Write
  IL_0012:  newobj      UserQuery+Bar..ctor
  IL_0017:  stloc.1     // y
  IL_0018:  ldarg.0     
  IL_0019:  ldloc.1     // y
  IL_001A:  call        UserQuery.Test
  IL_001F:  call        System.Console.Write
  </code></pre>