gpt4 book ai didi

c# - 在 IEnumerable 上下文之外使用协方差

转载 作者:太空狗 更新时间:2023-10-30 00:11:16 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Does C# support return type covariance?
Why can’t I implement an Interface this way?

考虑以下几点:

public interface IAnimal {
}

public class Animal : IAnimal {
}

public interface ICage {
IAnimal SomeAnimal {get;}
}

public class Cage : ICage{
public Animal SomeAnimal { get; set; }
}

我已经阅读了很多关于 IEnumerable 的协变和逆变的资料,但我不确定如何让上面的代码工作。我收到错误“Cage 没有实现接口(interface)成员 IAnimal”。由于它定义了比 IAnimal 定义更多的 Animal,因此协方差似乎应该照顾我。

我错过了什么?提前致谢。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com