gpt4 book ai didi

C# 列表到 ICollection

转载 作者:行者123 更新时间:2023-11-30 19:01:49 25 4
gpt4 key购买 nike

这很奇怪,我试图在我的构造函数中使用 List 初始化我的 ICollection 并且发生了这种情况:

Schedules = new List<BookingSchedule>(); //OK
CateringItems = new List<CateringItem>(); //Not

属性:

public virtual ICollection<BookingSchedule> Schedules { get; set; }
public virtual ICollection<BookedCateringItem> CateringItems { get; set; }

错误:

Error   1   Cannot implicitly convert type
'System.Collections.Generic.List<MyApp.Models.CateringItem>' to
'System.Collections.Generic.ICollection<MyApp.Models.BookedCateringItem>'.
An explicit conversion exists (are you missing a cast?)

我看不出两者之间的区别。我要疯了想弄清楚这一点。有什么想法吗?

最佳答案

您只能转换List<T1>ICollection<T2>如果类型 T1 和 T2 相同。或者,您可以转换为非通用 ICollection

ICollection CateringItems = new List<CateringItem>(); // OK

关于C# 列表到 ICollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21040755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com