[C#]Dictionary的foreach使用KeyValuePair

最近剛好要用到Dictionary來記錄一些東西..

但不知道如何使用foreach把每一個Dictionary裡的東西抓出來..

去找了一下msdn,把它記在Blog裡...分享給大家呀..

找到了一些資訊:

foreach (KeyValuePair<int, string> kvp in myDictionary) {...}

看到這句就大概可以猜出來了,就是用KeyValuePair,用範例介紹如何使用..

asp.net(c#)
TestKeyValuePair.aspx.cs

  1. using System;   
  2. using System.Data;   
  3. using System.Configuration;   
  4. using System.Collections;   
  5. using System.Web;   
  6. using System.Web.Security;   
  7. using System.Web.UI;   
  8. using System.Web.UI.WebControls;   
  9. using System.Web.UI.WebControls.WebParts;   
  10. using System.Web.UI.HtmlControls;   
  11. using System.Collections.Generic;   
  12.   
  13. public partial class TestKeyValuePair : System.Web.UI.Page   
  14. {   
  15.     protected void Page_Load(object sender, EventArgs e)   
  16.     {   
  17.         Dictionary<string, string> dic = new Dictionary<string, string>();   
  18.   
  19.         dic.Add("Name", "puma");   
  20.         dic.Add("Blog", "F6 Team");   
  21.         dic.Add("Group", "Dotblogs");   
  22.   
  23.         foreach (KeyValuePair<string, string> item in dic)   
  24.         {   
  25.             Response.Write(string.Format("{0} : {1}<br/" + ">", item.Key, item.Value));   
  26.         }   
  27.   
  28.     }   
  29. }  

 

執行結果:

參考網址:

http://msdn.microsoft.com/en-us/library/xfhwa508(VS.80).aspx

上一篇: [Serializable]在C#中的作用-NET 中的对象序列化
下一篇: 正则表达式30分钟入门教程
fancy050821
博客等级 码龄19年 5粉丝 59原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值