[英]Generics and System.Collections
After moving to .NET 2.0+ is there ever a reason to still use the systems.Collections namespace (besides maintaining legacy code)? 迁移到.NET 2.0+后,有没有理由继续使用systems.Collections命名空间(除了维护遗留代码)? Should the generics namespace always be used instead?
是否应该始终使用泛型命名空间?
For the most part, the generic collections will perform faster than the non-generic counterpart and give you the benefit of having a strongly-typed collection. 在大多数情况下,泛型集合的执行速度将比非泛型集合更快,并且可以为您提供强类型集合的好处。 Comparing the collections available in System.Collections and System.Collections.Generic, you get the following "migration":
比较System.Collections和System.Collections.Generic中可用的集合,您将获得以下“迁移”:
Non-Generic Generic Equivalent ------------------------------------------------------------ ArrayList List<T> BitArray N/A CaseInsensitiveComparer N/A CollectionBase Collection<T> Comparer Comparer<T> DictionaryBase Dictionary<TKey,TValue> Hashtable Dictionary<TKey,TValue> Queue Queue<T> ReadOnlyCollectionBase ReadOnlyCollection<T> SortedList SortedList<TKey,TValue> Stack Stack<T> DictionaryEntry KeyValuePair<TKey,TValue> ICollection N/A (use IEnumerable<T> or anything that extends it) IComparer IComparer<T> IDictionary IDictionary<TKey,TValue> IEnumerable IEnumerable<T> IEnumerator IEnumerator<T> IEqualityComparer IEqualityComparer<T> IList IList<T>
ICollection is immutable (no members to change the contents of the collection) while ICollection<T> is mutable. ICollection是不可变的(没有成员可以更改集合的内容),而ICollection <T>是可变的。 This makes the interfaces similar in name only while ICollection and IEnumerable<T> differ by very little.
这使得接口在名称上相似,而ICollection和IEnumerable <T>相差很小。
From this list, the only non-generic classes that don't have a generic counterpart are BitArray and CaseInsensitiveComparer. 从该列表中,唯一没有通用对应的非泛型类是BitArray和CaseInsensitiveComparer。
In some circumstances the generic containers perform better than the old ones. 在某些情况下,通用容器的性能优于旧容器。 They should at least perform as well as the old ones in all circumstances.
在任何情况下,它们至少应该与旧的一样好。 And they help to catch programming errors.
它们有助于捕捉编程错误。 It's a rare combination of a more helpful abstraction and better performance, so there isn't much of a reason to avoid them.
这是一种更有用的抽象和更好的性能的罕见组合,因此没有太多理由可以避免它们。 Only if you are forced to by a crummy library you have to work with that was written before generics.
只有当你被迫使用一个糟糕的图书馆时,你必须使用它,这是在泛型之前写的。
I saw an interview with Anders Hejlsberg from the c# team and he was asked if there was anything he regretted with the previous releases of .net. 我看到了来自c#团队的Anders Hejlsberg的采访,他被问到是否有任何他对先前版本的.net感到遗憾。 Not having generics in asp.net 1.0 was the first thing he mentioned.
在asp.net 1.0中没有泛型是他提到的第一件事。 Not having it there meant they had to implement workarounds that would stick with the .net libraries and soon become legacy code.
没有它意味着他们必须实现坚持.net库并很快成为遗留代码的变通方法。
I never use the System.Collections namespace and from his statement this seem to be the correct path. 我从不使用System.Collections命名空间,并且在他的语句中,这似乎是正确的路径。
About the only bad thing I can think of when using Generics is variance, so for example, if you have a List<Person>
and you want to pass it to a method that takes List<object>
you can't because List<Person>
cannot be cast to List<object>
directly. 关于使用Generics时唯一可以想到的坏处就是方差,所以例如,如果你有一个
List<Person>
并且你想将它传递给一个采用List<object>
的方法,你就不能因为List<Person>
无法直接转换为List<object>
。
This problem is solved in .NET 4.0. 在.NET 4.0中解决了此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.