Error due to concrete class and interface not having matching return types for IEnumerable<T> and List<T>
c#, ienumerable, interface, list
Solution
You must respect the contract when implementing an interface:
public class Audit : IAudit
{
public IEnumerable<ICategory> Categories { get; set; }
public IEnumerable<IAuditAnswer> Answers { get; set; }
}
In the interface the 2 properties are defined as `IEnumerable<T>`, so in your implementing class you should use the same type instead of `List<T>`.
Problem
I have the below interface and concrete class. I am getting an error of: 'DAL.Model.Audit.Categories' cannot implement 'DAL.Interfaces.IAudit.Categories' because it does not have the matching return type of 'System.Collections.Generic.IEnumerable'. Could anyone please explain what I am doing wrong here? ``` 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; } } ```