简体   繁体   中英

C# cast from IEnumerable<T> to T

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;
    }
}

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; }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM