Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用字典项的最佳方法是使用列表的优点_C#_.net_List_Dictionary - Fatal编程技术网

C# 使用字典项的最佳方法是使用列表的优点

C# 使用字典项的最佳方法是使用列表的优点,c#,.net,list,dictionary,C#,.net,List,Dictionary,我想像在List泛型类中一样使用字典项, e、 g 但在字典里是不可能的,就像上面的e方法 Dictionary<string, HtmlInputImage> smartPenImageDictionary; Dictionary智能词典; 我的意思是我知道了字典项的关键项。。但是我想要的是,我想从列表的开始一直到列表的结尾。在字典上迭代会产生KeyValuePairs。只需访问相应变量的键和值成员。在字典上迭代会产生KeyValuePairs。只需访问相应变量的键和值成员。字

我想像在List泛型类中一样使用字典项, e、 g

但在字典里是不可能的,就像上面的e方法

Dictionary<string, HtmlInputImage> smartPenImageDictionary;
Dictionary智能词典;

我的意思是我知道了字典项的关键项。。但是我想要的是,我想从列表的开始一直到列表的结尾。

在字典上迭代会产生
KeyValuePair
s。只需访问相应变量的
成员。

在字典上迭代会产生
KeyValuePair
s。只需访问相应变量的
成员。

字典没有“开始”和“结束”-它是无序的

但是,您可以迭代键或值:

foreach (string key in smartPenImageDictionary.Keys)
{
    ...
}

foreach (HtmlInputImage image in smartPenImageDictionary.Values)
{
    ...
}
或键/值对:

foreach (KeyValuePair<string, HtmlInputImage> pair in smartPenImageDictionary)
{
    string key = pair.Key;
    HtmlInputImage image = pair.Value;
    ...
}
foreach(SmartDictionary中的KeyValuePair对)
{
string key=pair.key;
HtmlInputImage=pair.Value;
...
}

(当然,
var
会使最后一个例子变得更好。)

字典没有“开始”和“结束”——它是无序的

但是,您可以迭代键或值:

foreach (string key in smartPenImageDictionary.Keys)
{
    ...
}

foreach (HtmlInputImage image in smartPenImageDictionary.Values)
{
    ...
}
或键/值对:

foreach (KeyValuePair<string, HtmlInputImage> pair in smartPenImageDictionary)
{
    string key = pair.Key;
    HtmlInputImage image = pair.Value;
    ...
}
foreach(SmartDictionary中的KeyValuePair对)
{
string key=pair.key;
HtmlInputImage=pair.Value;
...
}
(当然,
var
会使最后一个案例变得更好。)

类似这样:

foreach (KeyValuePair<string, HtmlInputImage> kvp in smartPenImageDictionary)
{
   Console.WriteLine("Value " + kvp.Value);
}
foreach(SmartDictionary中的KeyValuePair kvp)
{
Console.WriteLine(“值”+kvp.Value);
}
类似这样的内容:

foreach (KeyValuePair<string, HtmlInputImage> kvp in smartPenImageDictionary)
{
   Console.WriteLine("Value " + kvp.Value);
}
foreach(SmartDictionary中的KeyValuePair kvp)
{
Console.WriteLine(“值”+kvp.Value);
}

我假设您希望它们按顺序排列,就像
IList提供的那样。据我所知,
字典
没有订单保证,所以你需要

mydictionary.ToList();

但是,当您添加和删除项目,甚至再次调用此项时,它可能会发生更改。一种解决方案是编写您自己的
集合
,其中有一个您可以使用的自定义索引器。

我假设您希望它们按顺序排列,就像
IList
提供的那样。据我所知,
字典
没有订单保证,所以你需要

mydictionary.ToList();

但是,当您添加和删除项目,甚至再次调用此项时,它可能会发生更改。一个解决方案是编写您自己的
集合
,其中有一个自定义索引器,您可以使用。

我不确定您想要实现什么,但以下是使用字典可以实现的常见功能

IDictionary<Int32, String> dictionary = new Dictionary<Int32, String>();

// Iterate over all key value pairs.
foreach (KeyValuePair<Int32, String> keyValuePair in dictionary)
{
    Int32  key = keyValuePair.Key;
    String value = keyValuePair.Value;
}

// Iterate over all keys.
foreach (Int32 key in dictionary.Keys)
{
    // Get the value by key.
    String value = dictionary[key];
}

// Iterate over all values.
foreach (String value in dictionary.Values)
{
}
IDictionary dictionary=newdictionary();
//迭代所有键值对。
foreach(字典中的KeyValuePair KeyValuePair)
{
Int32 key=keyValuePair.key;
字符串值=keyValuePair.value;
}
//迭代所有键。
foreach(dictionary.Keys中的Int32键)
{
//通过键获取值。
字符串值=字典[键];
}
//迭代所有值。
foreach(dictionary.Values中的字符串值)
{
}

我不确定你想要实现什么,但以下是使用字典可以实现的常见功能

IDictionary<Int32, String> dictionary = new Dictionary<Int32, String>();

// Iterate over all key value pairs.
foreach (KeyValuePair<Int32, String> keyValuePair in dictionary)
{
    Int32  key = keyValuePair.Key;
    String value = keyValuePair.Value;
}

// Iterate over all keys.
foreach (Int32 key in dictionary.Keys)
{
    // Get the value by key.
    String value = dictionary[key];
}

// Iterate over all values.
foreach (String value in dictionary.Values)
{
}
IDictionary dictionary=newdictionary();
//迭代所有键值对。
foreach(字典中的KeyValuePair KeyValuePair)
{
Int32 key=keyValuePair.key;
字符串值=keyValuePair.value;
}
//迭代所有键。
foreach(dictionary.Keys中的Int32键)
{
//通过键获取值。
字符串值=字典[键];
}
//迭代所有值。
foreach(dictionary.Values中的字符串值)
{
}