如何在这种情况下返回List (How can I return List in this scenario)

编程入门 行业动态 更新时间:2024-10-26 21:22:24
如何在这种情况下返回List (How can I return List in this scenario)

让我开始说我对使用接口很新颖。

我正在编写一个方法(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.

更多推荐

本文发布于:2023-07-22 19:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222816.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在这种情况下   List   scenario   return

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!