小编Xin*_*nbi的帖子

无法将源类型"List <Person>"转换为IList <ISomething>

这可能是一个非常简单的问题,但对我来说没有意义.

鉴于此类:

public class Person : ICloneable {
    public object Clone()
    {
        Console.WriteLine("Hello, world");
        return new Person();
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么这样可以?

List<Person> people = new List<Person> { new Person() };

IEnumerable<ICloneable> clonables = people;
Run Code Online (Sandbox Code Playgroud)

但这不是吗?

List<Person> people = new List<Person> { new Person() };

IList<ICloneable> clonables = people;
Run Code Online (Sandbox Code Playgroud)

为什么我可以分配给IEnumerable IClonable,而不是IList ICloneable?

.net c# casting

7
推荐指数
2
解决办法
8138
查看次数

标签 统计

.net ×1

c# ×1

casting ×1