C# cast from IEnumerable<T> to T

124 Views Asked by At

Can anyone explain that :

  • compile OK
  • run-time KO

I already checked source code for IEnumerable<> & IEnumerable and found nothing FYI this doesn't work with List

class Program
{
    public class Person
    {
        public long Age { get; set; }
    }

    static void Main(string[] args)
    {
        IEnumerable<Person> list = new List<Person>();
        ((Person)list).Age = 5;
    }
}
2

There are 2 best solutions below

6
sofiene reghima On

I tried something not too useful but "correct" for both C# compiler & runtime

I got my answer

    public class Person
    {
        public long Age { get; set; }
    }

    static void Main(string[] args)
    {
        IEnumerable<Person> list;
        list = new Father();
        ((Father)list).Age = 5;
    }

    public class Father : Person, IEnumerable<Person>
    {
        public IEnumerator<Person> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }
}

Example

9
Marco Salerno On

There isn't a check at compile time for interface cast to class.

As you can see you can even compile that:

class Program
{
    static void Main(string[] args)
    {
        IPizza<Pasta> ao = new Pizza<Pasta>();
        ((Pasta)ao).MyProperty = 10;
    }
}

public interface IPizza<T>
{

}

public class Pizza<T> : IPizza<T>
{

}

public class Pasta
{
    public int MyProperty { get; set; }
}

Interface casted to class will be evaluated in runtime.

Infact, if you use as type Pizza<Pasta> instead of IPizza<Pasta> it will give you a compile error:

class Program
{
    static void Main(string[] args)
    {
        Pizza<Pasta> ao = new Pizza<Pasta>();
        ((Pasta)ao).MyProperty = 10;
    }
}
public interface IPizza<T>
{

}

public class Pizza<T> : IPizza<T>
{

}

public class Pasta
{
    public int MyProperty { get; set; }
}