在字典中循环 [英] Loop in Dictionary

查看:137
本文介绍了在字典中循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个:

  foreach(MyVic中的KeyValuePair< String,String>条目)
{
//做某事与entry.Value或entry.Key

}

问题是我不能更改entry.Value或entry.Key的值。

我的问题是,如何在循环时更改值或键通过字典?
而且,字典是否允许重复的键?如果是,我们该如何避免?
谢谢

解决方案

虽然在循环使用字典中的项目时,您不能更改字典条目的值,您可以修改该值的属性,如果它是引用类型的实例。



例如,

  public class MyClass 
{
public int SomeNumber {get; set;}
}

foreach(KeyValuePair< string,MyClass> myDict中的条目)
{
entry.Value.SomeNumber = 3; //是好的
myDict [entry.Key] = new MyClass(); //不正确
}

尝试修改字典(或任何集合),而循环遍历其元素将导致 InvalidOperationException 表示集合已修改。



要回答您的具体问题, / p>


我的问题是,如何在循环使用字典时更改值或键?


两者的方法将几乎相同。你可以循环一个字典的副本,如Anthony Pengram在他的回答中所说,或者你可以循环一次通过所有的项目,以弄清楚你需要修改哪些,然后再循环通过这些项目的列表:

 列表与LT;串GT; keysToChange = new List< string>(); 
foreach(KeyValuePair< string,string> myDict中的条目)
{
if(...)//某些检查是否是要对
执行的项目{
keysToChange.Add(entry.Key);
}
}

foreach(keysToChange中的字符串键)
{
myDict [key] =new value;

//或重命名一个键
myDict [new key] = myDict [key];
myDict.Remove(key);
}




而且,字典是否允许重复的键?如果是的话,我们该如何避免?


字典不允许重复的键。如果您想要收集< string,string> 对,请查看 NameValueCollection


I use this:

foreach(KeyValuePair<String,String> entry in MyDic)
  {
      // do something with entry.Value or entry.Key

  }

The problem is that I can't change the value of entry.Value or entry.Key

My question is that how can i change the value or key when looping through a dictionary? And, does dictionary allow duplicated key? And if yes, how can we avoid ? Thank you

解决方案

You cannot change the value of a dictionary entry while looping through the items in the dictionary, although you can modify a property on the value if it's an instance of a reference type.

For example,

public class MyClass 
{
    public int SomeNumber { get; set;}
}

foreach(KeyValuePair<string, MyClass> entry in myDict)
{
    entry.Value.SomeNumber = 3; // is okay
    myDict[entry.Key] = new MyClass(); // is not okay
}

Trying to modify a dictionary (or any collection) while looping through its elements will result in an InvalidOperationException saying the collection was modified.

To answer your specific questions,

My question is that how can i change the value or key when looping through a dictionary?

The approach to both will be pretty much the same. You can either loop over a copy of the dictionary as Anthony Pengram said in his answer, or you can loop once through all the items to figure out which ones you need to modify and then loop again through a list of those items:

List<string> keysToChange = new List<string>();
foreach(KeyValuePair<string, string> entry in myDict)
{
    if(...) // some check to see if it's an item you want to act on
    {
        keysToChange.Add(entry.Key);
    }
}

foreach(string key in keysToChange)
{
   myDict[key] = "new value";

   // or "rename" a key
   myDict["new key"] = myDict[key];
   myDict.Remove(key);
}

And, does dictionary allow duplicated key? And if yes, how can we avoid ?

A dictionary does not allow duplicate keys. If you want a collection of <string, string> pairs that does, check out NameValueCollection.

这篇关于在字典中循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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