在哪里可以了解各种类型的.NET列表? [英] Where can I learn about the various types of .NET lists?

查看:124
本文介绍了在哪里可以了解各种类型的.NET列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道一个好的资源来简洁地解释C#中可用的不同类型的列表,以及它们的用法是否合适?



例如,List,Hashtable,Dictionaries

解决方案



我不知道什么时候应该使用什么。 div>

这些不是所有的列表,虽然他们都是集合。



非通用集合(API是 object



这些主要位于 System.Collections 命名空间:




  • ArrayList :由数组支持的项目列表快速随机读/快速添加到尾端,如果缓冲区不需要调整大小。

  • Hashtable :从键映射到值键是唯一的,值不必是使用GetHashCode方法来实现近O(1)读/写访问(除了所有项目具有相同散列或后备存储器需要重建的讨厌的情况)。迭代在键/值对上给出不可预测的顺序。

  • SortedList :像一个Hashtable,但条目总是按逐个键的顺序返回。存储为键/值对列表。

  • Stack :后进先出收藏

  • Queue :先进先出收集

  • 数组:固定大小的O(1)随机访问;非泛型,但也具有强类型表单



通用集合。



这些类型大多数在 System.Collections.Generic命名空间




$ b b

可能最重要的集合接口 IEnumerable (和 IEnumerable< T> )。这表示一系列项目,很像Stream表示一个字节序列。没有随机访问,只是前向阅读。 LINQ to Objects基于这一点,几乎所有的集合类型实现它。


Does anyone know a good resource to concisely explain the different types of lists available in C# and when their usage is appropriate?

For example, List, Hashtable, Dictionaries etc.

I'm never quite sure when I should be using what.

解决方案

These aren't all lists, although they're all collections. Here's a quick summary.

Non-generic collections (API is in terms of object. Values types are boxed.

These are mostly in the System.Collections namespace:

  • ArrayList: A list of items, backed by an array. Fast random read/write access. Fast add to the tail end, if the buffer doesn't need resizing.
  • Hashtable: Map from key to value. Keys are unique, values don't have to be. Uses the GetHashCode method to achieve near O(1) read/write access (aside from nasty cases where all items have the same hash, or the backing store needs rebuilding). Iterating over the key/value pairs gives an unpredictable order. (Well, effectively unpredictable.)
  • SortedList: Like a Hashtable, but the entries are always returned in sorted-by-key order. Stored as a list of key/value pairs.
  • Stack: Last-in-first-out collection
  • Queue: First-in-first-out collection
  • Array: Fixed-size O(1) random-access; non-generic, but has strongly typed forms as well

Generic collections. (Strongly-typed API, will not box value types (assuming suitable T).

These are mostly in the System.Collections.Generic namespace:

Possibly the most important collection interface is IEnumerable (and IEnumerable<T>). This represents a sequence of items much like a Stream represents a sequence of bytes. There is no random access, just forward-reading. LINQ to Objects is based on this, and pretty much all collection types implement it.

这篇关于在哪里可以了解各种类型的.NET列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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