I suggest you ...

Remove IEnumerable-Bug

This code compiles and gives runtime errors, even if it would be easy for a compiler to figure this out:

interface II{}
class A{}

IEnumerable<II> enumerable=...;

foreach(A a in enumerable)
....

class A is no base class of II, it does not even implement it. So the compiler uses the c# 1.0-enumerable-interface which returns an object, and then tries to cast that object into an A, which - suprise - does fail and causes an runtime exception.

Maybe such behaviour is useful in edge cases, but in normal scenarios it just triggers runtime errors. During refactoring most of the time the compiler will help, what you can do, and what not, but not in this case.

Imagine you usually use class A, and at some places an IEnumerable<A>. Now you try to refacture your code, and replace most uses of A by an Interface IA, which A implements. IEnumerable<A> is replaced by IEnumerable<IA> and everything seems to work, until sometime you introduce a new class LeanA:IA.

then the line
IEnumerable<IA> ias=...
foreach(A a in ias)

will break with an runtime exception.
It would be nice, to have at least a compiler switch to disable this strange behabviour.

1 vote
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Steffen GuhlemannSteffen Guhlemann shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    0 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...

      Feedback and Knowledge Base