不能使用带字典的foreach循环? [英] Can't Use a foreach Loop With Dictionaries?

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

问题描述

我正在使用dictonary存储值并为我使用foreach循环检索它



错误1 foreach语句无法操作类型为'CSharpBasics.Dictionary< string,int>'的变量因为'CSharpBasics.Dictionary< string,int>'不包含'GetEnumerator'的公共定义C:\ Users \ Divya \Documents\Visual Studio 2012 \Projects\CSharpBasics\CSharpBasics\Program.cs 25 13 CSharpBasics 





我的代码是



  public   static   void  Main( string  [] args)
{
Dictionary< string,Int32> AuthorList = new Dictionary< string,Int32>();

AuthorList.Add( Mahesh Chand 35 );
AuthorList.Add( Mike Gold 25 );
AuthorList.Add( Praveen Kumar 29 );
AuthorList.Add( Raj Beniwal 21 );
AuthorList.Add( Dinesh Beniwal 84 );


// 读取所有数据
控制台.WriteLine( 作者列表);

foreach (KeyValuePair< string,string> author in AuthorList)
{
Console.WriteLine( Key = {0},Value = {1}
author.Key,author.Value);
}
}







pls lemme 我很快就知道了答案! !!!!

解决方案

  foreach (KeyValuePair< string,  string   > 作者作者列表中)





使用



  foreach (KeyValuePair< string,  int   > 作者 作者列表)


I am using dictonary to store the values and retrive it for the i am using foreach loops

Error   1   foreach statement cannot operate on variables of type 'CSharpBasics.Dictionary<string,int>' because 'CSharpBasics.Dictionary<string,int>' does not contain a public definition for 'GetEnumerator'  C:\Users\Divya\Documents\Visual Studio 2012\Projects\CSharpBasics\CSharpBasics\Program.cs   25  13  CSharpBasics



my code is

public static void Main(string[] args)
{
    Dictionary<string, Int32> AuthorList = new Dictionary<string, Int32>();

    AuthorList.Add("Mahesh Chand", 35);
    AuthorList.Add("Mike Gold", 25);
    AuthorList.Add("Praveen Kumar", 29);
    AuthorList.Add("Raj Beniwal", 21);
    AuthorList.Add("Dinesh Beniwal", 84);


    // Read all data
    Console.WriteLine("Authors List");

    foreach (KeyValuePair<string, string> author in AuthorList)
    {
        Console.WriteLine("Key = {0}, Value = {1}",
                    author.Key, author.Value);
    }
}




pls lemme me know the answer soon!!!!!

解决方案

foreach (KeyValuePair<string,string> author in AuthorList)



use

foreach (KeyValuePair<string,int> author in AuthorList)


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

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