C# 由于具体类和接口没有与IEnumerable匹配的返回类型而导致错误<;T>;和列表<;T>;
我有下面的接口和具体的类 我得到一个错误: “DAL.Model.Audit.Categories”无法实现“DAL.Interfaces.IAudit.Categories”,因为它没有匹配的返回类型“System.Collections.Generic.IEnumerable” 谁能解释一下我做错了什么C# 由于具体类和接口没有与IEnumerable匹配的返回类型而导致错误<;T>;和列表<;T>;,c#,list,interface,ienumerable,C#,List,Interface,Ienumerable,我有下面的接口和具体的类 我得到一个错误: “DAL.Model.Audit.Categories”无法实现“DAL.Interfaces.IAudit.Categories”,因为它没有匹配的返回类型“System.Collections.Generic.IEnumerable” 谁能解释一下我做错了什么 public interface IAudit { IEnumerable<ICategory> Categories { get; set; } IEnumer
public interface IAudit
{
IEnumerable<ICategory> Categories { get; set; }
IEnumerable<IAuditAnswer> Answers { get; set; }
}
public class Audit : IAudit
{
public List<ICategory> Categories { get; set; }
public List<IAuditAnswer> Answers { get; set; }
}
公共接口IAudit
{
IEnumerable Categories{get;set;}
IEnumerable Answers{get;set;}
}
公共类审计:IAudit
{
公共列表类别{get;set;}
公共列表答案{get;set;}
}
在实施接口时,您必须遵守合同:
public class Audit : IAudit
{
public IEnumerable<ICategory> Categories { get; set; }
public IEnumerable<IAuditAnswer> Answers { get; set; }
}
公共类审核:IAudit
{
公共IEnumerable类别{get;set;}
公共IEnumerable答案{get;set;}
}
在接口中,这两个属性被定义为
IEnumerable
,因此在实现类中,您应该使用相同的类型,而不是List
,您可以阅读这篇非常有用的文章来了解原因:
不幸的是,强类型集合本身也有缺点
这不起作用的原因是因为
IList
比IEnumerable
更专业化
在你的脑海中,你(我猜)认为
IList
实现了IEnumerable
,所以“为什么我不能直接使用它?”而真正的答案在于。感谢链接,这有助于解释为什么我的假设是错误的。