title: foreach won't type check even when enumerated type is sealed
body: <p>I have a similar problem to <a href="https://stackoverflow.com/questions/3389327/why-does-foreach-skip-compile-time-type-checking-on-interface-types">this question</a>. But instead my <code>SomeClass</code> implements <code>SomeInterface</code>. In this case, even if I mark <code>SomeClass</code> as <code>sealed</code>, compiler time type check still don't kick in. Example below.</p>
  <h3>Question 1: Why wouldn't the compiler give an error as the type of the loop variable isn't compatible with the type of elements of the source enumerable?</h3>
  <p>Having compiler type check is important to me, as I'm refactoring a big project, changing the enumeration source (<code>source</code> in a <code>foreach (SomeClass foo in source)</code>) from <code>IEnumerable&lt;SomeClass&gt;</code> to <code>IEnumerable&lt;SomeInterface&gt;</code>. Sometime I forget to change the enumerator type declaration in the loop from <code>SomeClass</code> to <code>SomeInterface</code>, and the project still compiles. That would break things really bad!!</p>
  <h3>Question 2: What suggestion do you have to make this refactoring type safe?</h3>
  <p>Note that I can't change loop variable type to be implicit <code>var</code> because it's exiting code base. But I am able to change the declaration of the interface and its concrete implementation classes.</p>
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          IEnumerable&lt;SomeInterface&gt; someFoos = new SomeInterface[] { new SomeOtherClass(), new SomeOtherClass(), new SomeOtherClass() };
          foreach (SomeClass foo in someFoos)
          {
              foo.ToString(); // runtime InvalidCastException
          }
      }
  }
  
  interface SomeInterface { }
  
  sealed class SomeClass : SomeInterface
  {
      public override string ToString()
      {
          return &quot;SomeClass&quot;;
      }
  }
  
  class SomeOtherClass : SomeInterface
  {
      public override string ToString()
      {
          return &quot;SomeOtherClass&quot;;
      }
  }
  </code></pre>