c# - 在 C# 中,将接口(interface)列表转换为对象列表的推荐方法是什么?

标签 c# collections casting

我有返回的代码和 IVehicle 接口(interface)数组

  IEnumerable<IVehicle> allVehicles = GetAllVehicles();

我知道所有这些对象都是 Car 对象。什么是正确的施法方式
IEnumerable<IVehicle>


IEnumerable<Car>

因为我需要将这些传递给另一个需要 <Enumerable<Car> 的函数

最佳答案

你可以像这样转换它:

var allCars = allVehicles.Cast<Car>();

关于c# - 在 C# 中,将接口(interface)列表转换为对象列表的推荐方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33920222/

相关文章:

c# - 将 System.Web.Mail 功能移植到 System.Net.Mail

java 集合和数组列表

java - 如何将 int 值添加到 Set<Integer> (HashSet<Integer>)?

c# - 将 HttpPostedFileBase 转换到流

swift - 类型转换任何?为 Int、String 或 Bool

c# - 具有动态列组的 LINQ GroupBy

C# ? : operator

c# - JSON将不同的命名对象反序列化为集合c#

C# 将货币字符串转换为 Double

c# - 在 Azure WebRole 上自动启动 WCF