当用Enumerator遍历Dictionary<string, string>时,遇到返回值为空的情况,求解。
                          //Get the source code of the open page.
                Dictionary<string, string> hrefs = UIMethods.GetDivHrefs(codeview, this.expectedContentId);
                //这里为何得到的enumerator都为空,非常迷惑
                Dictionary<string, string>.Enumerator enumerator = hrefs.GetEnumerator();
                
                if (null != enumerator.Current.Value) 
                {
                    ///。。
                 }           
          

解决方案 »

  1.   

    foreach (KeyValuePair<string, string> kvp in hrefs)
    kvp.Value
      

  2.   

     foreach( KeyValuePair<int,int> entry in hrefs )
                {
                    Console.WriteLine(
                        string.Format("key:{0}\nvalues:{1}\n",
                                      entry.Key,
                                      entry.Value
                                     )
                    );
                }
      

  3.   

    不想用foreach,我只是要取第一个就是就行了。
      

  4.   

    不想用foreach,我只是要取第一个就是就行了。
      

  5.   

    lz 没有明白Enumerator 是什么
    Enumerator 默认情况下它指向的是第一个元素之前,而不是第一个元素,所以current 获取的是空
    楼上已经告诉你了要先movenext
      

  6.   

    第一个当然是空了,永远为空,只有移动到下一个位置才可能不为空,正如二楼所说,MoveNext()调用后判断一下返回值是否为true,来确定是否获取到数据,及时只要第一条数据,也至少要调用一下MoveNext()才行。