title: Why can't the compiler prevent these runtime cast exceptions?
body: <pre><code>class Program
      {
          static void Main(string[] args)
          {
              DateTime newYears = new DateTime(2013, 1, 1);
              PromoteEmployee(newYears);
          }
  
          public static void PromoteEmployee(object o)
          {
              // At this point, the compiler doesn't know exactly what type of object o refers to. So the compiler allows the code to compile...
  
              Employee e = (Employee)o; // this results in invalid cast exception at runtime
          }
      }
  
      class Employee
      {
  
      }
  </code></pre>
  
  <p>But I think the compiler does know or it can easily know by looking at all places where this method is being referenced as to what type is being passed to PromoteEmployee (at least for such cases where no runtime conditions decide what type gets passed in)</p>
  
  <p>So why should casting exceptions be left to occur at runtime when the compiler with some extra logic easily pick them up ? </p>