C# .NET 中数据的最佳集合

发布于 2024-12-09 05:56:57 字数 157 浏览 0 评论 0原文

我必须存储这些数据:

 "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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

掩于岁月 2024-12-16 05:56:57

尝试 System.Collections.Generic.Dictionary

你可以像这样使用它:

var values = new Dictionary<string, int>
{
    { "AL", 1997 },
    { "AK", 1977 },
    ...
    { "WY", 1997 },
};

Console.WriteLine(values["AK"]);  // Writes 1977

Try System.Collections.Generic.Dictionary<TKey, TValue>.

You would use it like this:

var values = new Dictionary<string, int>
{
    { "AL", 1997 },
    { "AK", 1977 },
    ...
    { "WY", 1997 },
};

Console.WriteLine(values["AK"]);  // Writes 1977
半暖夏伤 2024-12-16 05:56:57

这是一个典型的键->值存储问题,因此您最好的选择是

SortedList<string, int> 

Dictionary<string, int>     

由于这里的问题是哪一个是最合适的,所以您可以:

在两者之间进行选择取决于您的使用情况。如果您要一次实例化所有列表,那么 SortedList 就是您的答案,因为它使用更少的内存并且比 Dictionary (SortedDictionary) 更快一些。如果您打算插入或删除项目,那么“词典”应该是您的选择,因为它速度更快一些。

来源,最初来自 MSDN

This is a typical key -> value storage problem, so your best option would be

SortedList<string, int> 

or

Dictionary<string, int>     

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

ま柒月 2024-12-16 05:56:57

首先,根据经验,您不应该对已知值使用非泛型集合类型,除非您使用 2.0 之前的 .NET 版本

对于唯一键

Dictionary<string,int32> or Dictionary<string,string>

对于非唯一键

List<KeyValuePair<string,int32>> or List<KeyValuePair<string,string>>

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

Dictionary<string,int32> or Dictionary<string,string>

For non-unique keys

List<KeyValuePair<string,int32>> or List<KeyValuePair<string,string>>
浅听莫相离 2024-12-16 05:56:57

如果您的键是唯一的,请使用 Dictionary

Use a Dictionary<string, int> if your keys are unique.

谜兔 2024-12-16 05:56:57

如果您需要从两位数字符串中查找日期,可以使用 System.Collections.Generic 中的 Dictionary

If you need to look up the date from the two-digit string, you could use Dictionary<string, int> from System.Collections.Generic.

屌丝范 2024-12-16 05:56:57

考虑到限制详细信息,Dictionary 似乎是最佳选择

Dictionary<string,Int32> seems to be the best choice given the limit details

囍孤女 2024-12-16 05:56:57

这取决于一些选项:

  • 作为包含整个段落的字符串(字符数组)
  • 作为数组、List 或每行的集合
  • 作为 Dictionary
  • code>,假设“AL”、“AK”等是唯一的,并且您希望在 DataSet

-->这完全取决于您对数据的处理方式、约束条件和要求。向我们提供更多信息,您将得到非常详细的答案。 <--

例如:如果您尝试将该数据存储为某些文本以在网页上显示,则为第一个选项。第二个选项作为解析用户输入的或从文件导入的某些文本的初始部分。如果您在代码中构建它,或者打算在代码或对象模型中查询或以其他方式执行逻辑,则使用第三个选项。如果您要往返于 ADO.NET 数据源,则为最后一个。还有更多......您可以使用包含 JBIG2 编码的抖动文本图像的 BinaryStream 对象。 ;)

That depends, some options:

  • As a string (array of chars) containing that whole paragraph
  • As an array, List or collection of each of those lines
  • As a Dictionary<string, int>, assuming the "AL", "AK" etc is unique and you want to look up the right-hand side based on the left-hand side
  • In a DataSet

--> 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. ;)

谁把谁当真 2024-12-16 05:56:57

如果您希望对其进行排序,可以使用 SortedDictionary

If you want it to be sorted, you could use a SortedDictionary

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文