让我开始说我对使用接口很新颖。
我正在编写一个方法(GetClaimDetails),它将返回有关医疗保险索赔的信息。 如果是声明类型A,它将返回ClaimDetailA类的列表。 如果声明类型B,ClaimDetailB类的返回列表。 这两个类都共享公共属性,但每个都有独特的属性。 通用属性在界面中实现。
我提取了一个名为IClaimDetail的接口并设置它们来实现它。 当我设置方法返回IClaimDetail并让它返回一个实现IClaimsDetail的对象的实例时,我得到编译器消息
'不能将类型'System.Collections.Generic.List DentalClaimDetail'隐式转换为'System.Collections.Generic.List IClaimDetail'
private static List<IClaimDetail> GetClaimDetailsB(string claimNumber, string connectionStringName) { var claimReportRows = new List<DentalClaimDetail>(); ..removed for brevity return claimReportRows; } public class DentalClaimDetail : IClaimDetail { ... }当一个方法返回一个接口时,你只需返回一个实现接口的对象。 正确? 我究竟做错了什么?
Let me start by saying I'm pretty new to using interfaces.
I'm writing a method (GetClaimDetails) that will return information about a medical insurance claim. If it is claim type A, it will return an list of the ClaimDetailA class. If claim type B, return list of ClaimDetailB class. Both of these classes share common properties but each have unique properties. The common properties are implemented in an interface.
I extracted an interface named IClaimDetail and set both to implement it. When i set the method to return IClaimDetail and have it return an instance of an object that implements IClaimsDetail, I get the compiler message
'Cannot implicitly convert type 'System.Collections.Generic.List DentalClaimDetail' to 'System.Collections.Generic.List IClaimDetail'
private static List<IClaimDetail> GetClaimDetailsB(string claimNumber, string connectionStringName) { var claimReportRows = new List<DentalClaimDetail>(); ..removed for brevity return claimReportRows; } public class DentalClaimDetail : IClaimDetail { ... }When a method returns an interface, you just return an object the implements the interface. Correct? What am I doing wrong?
最满意答案
var claimReportRows = new List<IClaimDetail>();只需改变这一点。 然后,您将能够将DentalClaimDetail插入到它中。
var claimReportRows = new List<IClaimDetail>();Just change this. Then you will be able to insert DentalClaimDetail into it still.
更多推荐
发布评论