C# .NET 中数据的最佳集合
我必须存储这些数据:
"AL" => 1997
"AK" => 1977
...
"WY" => 1997
在 .NET 中存储这些数据的最佳方法是什么?我应该只使用数组、arrayList 还是其他集合?
I have to store this data:
"AL" => 1997
"AK" => 1977
...
"WY" => 1997
What's the best way to store this in .NET? Shall I use just arrays, or arrayList, or another collection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
尝试
System.Collections.Generic.Dictionary
。你可以像这样使用它:
Try
System.Collections.Generic.Dictionary<TKey, TValue>
.You would use it like this:
这是一个典型的键->值存储问题,因此您最好的选择是
或
由于这里的问题是哪一个是最合适的,所以您可以:
在两者之间进行选择取决于您的使用情况。如果您要一次实例化所有列表,那么 SortedList 就是您的答案,因为它使用更少的内存并且比 Dictionary (SortedDictionary) 更快一些。如果您打算插入或删除项目,那么“词典”应该是您的选择,因为它速度更快一些。
来源,最初来自 MSDN
This is a typical key -> value storage problem, so your best option would be
or
As the question here is which is the best one suitable, here you go:
Choosing between the two depends on your usage. If you'll instantiate the list all at once then SortedList is your answer as it uses less memory and is a bit faster than Dictionary (SortedDictionary). If you intend to insert or delete items, then Dictionary should be your pick as it is a bit faster there.
Source, originally from MSDN
首先,根据经验,您不应该对已知值使用非泛型集合类型,除非您使用 2.0 之前的 .NET 版本
对于唯一键
对于非唯一键
First of all, by rule of thumb you should not use non-generic collection types for known values, unless you're using a version of .NET prior to 2.0
For unique keys
For non-unique keys
如果您的键是唯一的,请使用
Dictionary
。Use a
Dictionary<string, int>
if your keys are unique.如果您需要从两位数字符串中查找日期,可以使用
System.Collections.Generic
中的Dictionary
。If you need to look up the date from the two-digit string, you could use
Dictionary<string, int>
fromSystem.Collections.Generic
.考虑到限制详细信息,
Dictionary
似乎是最佳选择Dictionary<string,Int32>
seems to be the best choice given the limit details这取决于一些选项:
List
或每行的集合Dictionary
DataSet
中-->这完全取决于您对数据的处理方式、约束条件和要求。向我们提供更多信息,您将得到非常详细的答案。 <--
例如:如果您尝试将该数据存储为某些文本以在网页上显示,则为第一个选项。第二个选项作为解析用户输入的或从文件导入的某些文本的初始部分。如果您在代码中构建它,或者打算在代码或对象模型中查询或以其他方式执行逻辑,则使用第三个选项。如果您要往返于 ADO.NET 数据源,则为最后一个。还有更多......您可以使用包含 JBIG2 编码的抖动文本图像的
BinaryStream
对象。 ;)That depends, some options:
List
or collection of each of those linesDictionary<string, int>
, assuming the "AL", "AK" etc is unique and you want to look up the right-hand side based on the left-hand sideDataSet
--> It depends ENTIRELY on what you're doing with the data, what the constraints are, what the requirements are. Give us some more information and you'll get a very detailed answer. <--
E.g.: first option if you're trying to store that data as some text for display on a web page. The second option as the initial part of parsing some text entered by a user or imported from a file. The third option if you are building it in code, or intend to query or otherwise perform logic within the code, or an object model. The final one if you're going to/from an ADO.NET data source. There are more.. You could use a
BinaryStream
object containing a JBIG2-encoded, dithered image of the text. ;)如果您希望对其进行排序,可以使用 SortedDictionary
If you want it to be sorted, you could use a SortedDictionary