Why does compiler let this slip?
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/949798/foreach-can-throw-an-invalidcastexception">Foreach can throw an InvalidCastException?</a>  </p>
</blockquote>



<p>Consider the following block of code</p>

<pre><code>public class Base
{
}

public class DerivedLeft : Base
{
}

public class DerivedRight : Base
{
}

class Program
{
    static void Main(string[] args)
    {
        List&lt;Base&gt; list = new List&lt;Base&gt; { new DerivedLeft(), new DerivedRight() };
        foreach (DerivedLeft dl in list)
        {
            Console.WriteLine(dl.ToString());
        }
    }
}
</code></pre>

<p>Notice the cast from Base to DerivedLeft in foreach statement. This compiles fine (Visual Studio 2010), without any error or even warning.
Obviously, on the second loop-iteration we will get an InvalidCastException. 
If I was asked a question about reaction of compiler to such code, I would without a doubt say, that compiler won't let this go unnoticed and produce at least a warning.
But apparently it doesn't. So, why does the compiler let this slip through? </p>
