【发布时间】:2020-10-27 09:15:37
【问题描述】:
考虑一下 C# sn-p:
interface Contract
{
ICollection<string> Coll { get; }
}
class Implementation : Contract
{
public List<string> Coll { get; } //this declaration is mandatory for other reason
}
为什么实施不实施合同?
List<string> 实现ICollection<string>
真实案例
//Entity Framework
public partial class ApplicationUser : IdentityUser<Guid>
{
...
public virtual ICollection<Message> Messages { get; set; }
}
//Breeze.Sharp Entity
public partial class ApplicationUser : BaseEntity
{
...
public NavigationSet<Message> Messages
{
get { return GetValue<NavigationSet<Message>>(); }
}
}
如何找到两个类都实现的接口?
【问题讨论】:
-
只是因为
Derived<MyType>与Base<MyType>无关。无论如何,即使它会:接口是一个合同,必须完全履行。所以你的签名必须与界面完全匹配。 -
C# 9 引入covariant return types,可能对您的案例有所帮助
标签: c# inheritance interface breeze-sharp