c# - 将 IEnumerable 转换为 ICollection 时会发生什么

标签 c# casting ienumerable icollection

给定一个案例:

var collection = myEnumerable as ICollection<MyType>;

引擎盖下发生了什么? ICollection 有一个计数属性。此转换是通过枚举可枚举来获取计数,还是发生了更复杂的事情?

最佳答案

没有任何反应。如果myEnumerable确实是一个 ICollection<MyType> collection将包含它。否则它将是 null .就这么简单。

关于c# - 将 IEnumerable 转换为 ICollection 时会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12474557/

相关文章:

c# - 安装的 Win 服务未显示在服务管理器中

C# ASP.NET 简单选择问题

scala - Scala 中这种隐式转换的背后是什么?

sql - 如何将 String 值转换(或强制转换)为 Integer 值?

c# - 创建包含 List<int> 的字典条目

asp.net-mvc-3 - ASP.NET MVC 3 编辑器模板中的 IEnumerable 模型属性

c# - ASP.NET Core web.config requestFiltering 不覆盖 applicationhost.config

c# - 像 jQuery 那样制作动画?

c# - 使用反射在运行时转换类型?

c# - 如何在 GridView 中启用 IEnumerable 数据的自动排序?