开发者

Are there still cases when we would still use non-generic collections?

Besides the obvious reason of storing any type as an object and storing it in a non-generic collection, what other reasons are there for still using non-generic collections.

As of yet I have found no other purpose, other than backward compatibility with older software, for even keeping these types of collections in the .NET framework.

It seems like it would just create more work, because you're constantly boxing and un-boxing from type object to it's original type and vice-versa.

Also you run into the issue of sometimes not remembering what the original datatype of the object was to begin with (eg Double, Float, Decimal). This in fact has to be one of the problems I开发者_开发技巧 used to run into when developing in PHP.

Type safety seems like the way to go when developing modern-day applications, as I have created quite a few already and have never been forced to use any of them (non-generic collections).

Or maybe there are other reasons I'm not aware of that make these collections necessary? Obviously there included when trying to pass the certification exam for .NET for a reason. Just not sure why. Could someone please shed light on this subject?


I know of no reason other than legacy or third party support, basically, when you are forced to use them.


There are very few reasons to do this.

About the only argument I see, on occasion, to use one of the non-generic collections is that there is a potential performance difference in Hashtable vs. Dictionary<object,object>. This is really due to a difference in implementation, which is described in detail on MSDN here.

That being said, I'd still typically use Dictionary<T,U> - unless there was a very specific reason that Hashtable was providing some type of advantage, and this advantage could be proven via measurements on a real dataset. Overall, the performance is similar enough that I've rarely found issues in real world implementations. In most of my testing, Dictionary<T,U> outperforms Hashtable (which, I'm sure, was also Microsoft's findings, hence they decided to change to a chaining based implementation), but there are some cases when Hashtable can be faster.


From personal experience, I have never used non-generic collections since .NET 2.0 - only when I needed to demonstrate boxing or similar.

One use case could be having a collection which contains heterogeneous items although you can always use List<object> instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜