为什么在.NET中很多已命名的集合没有实现IEnumerable< T&GT ;? [英] Why do so many named collections in .NET not implement IEnumerable<T>?

查看:133
本文介绍了为什么在.NET中很多已命名的集合没有实现IEnumerable< T&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随便举个例子:

ConfigurationElementCollection

净有万吨不实现这些小 WhateverCollection 类的的IEnumerable< T> ,这意味着我不能使用Linq与他们的对象开箱。
连的LINQ之前,你会认为他们会想使使用泛型(其中引入所有的方式早在C#2我相信)

.Net has tons of these little WhateverCollection classes that don't implement IEnumerable<T>, which means I can't use Linq to objects with them out of the box. Even before Linq, you'd think they would have wanted to make use of generics (which were introduced all the way back in C# 2 I believe)

看来我在这些恼人的小集合类型运行所有的时间。
有一些技术原因?

It seems I run across these annoying little collection types all the time. Is there some technical reason?

推荐答案

答案是在问题的标题:命名集合。这是你不得不做的集合类型安全的泛型面世前的样子。有在代码中有很多人可以追溯到.NET 1.x中,特别的WinForms。有没有合理的方式使用泛型重写它们,这将打破太多现有的代码。

The answer is in the question title: "named collections". Which is the way you had to make collections type-safe before generics became available. There are a lot of them in code that dates back to .NET 1.x, especially Winforms. There was no reasonable way to rewrite them using generics, that would have broken too much existing code.

所以命名集合类型是类型安全的,但擦System.Collections中是.IEnumerator.Current,Object类型的属性。您可以通过使用OfType()或CAST()Linqify这些集合。

So the named collection type is type safe but the rub is System.Collections.IEnumerator.Current, a property of type Object. You can Linqify these collections by using OfType() or Cast().

这篇关于为什么在.NET中很多已命名的集合没有实现IEnumerable&LT; T&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆