C#中Dictionary几种遍历的实现代码_实用技巧

复制代码 代码如下:

 Dictionary<string,string> list=new Dictionary<string,string>;
//3.0以上版本
foreach(var item in list)
{
      Console.WriteLine(item.Key+item.Value);
}
//KeyValuePair<T,K>
foreach(KeyValuePair<string,string> kv in list)
{
      Console.WriteLine(kv.Key+kv.Value);
}
//通过键的集合取
foreach(string key in list.Keys)
{
      Console.WriteLine(key+list[key]);
}
//for循环遍历
List<string> test=new List<string>(list.Keys);
for(int i=0;i<list.Count;i++)
{
      Console.WriteLine(test[i]+list[test[i]]);
}
 

时间: 2024-09-17 03:54:03

C#中Dictionary几种遍历的实现代码_实用技巧的相关文章

asp.net下遍历页面中所有的指定控件的代码_实用技巧

1.遍历页面中所有的TextBox,并将值设置成String.Empty 复制代码 代码如下: for (int j = 0; j < this.Controls.Count; j++) { foreach (object o in Page.Controls[j].Controls) { if (o is TextBox) { TextBox txt = (System.Web.UI.WebControls.TextBox)o; txt.Text = String.Empty; } } } 2

C#和asp.net中链接数据库中参数的几种传递方法实例代码_实用技巧

复制代码 代码如下: #region 参数传递方法第一种     //参数设置方法(第一种)      //SqlParameter sp = new SqlParameter("@Name", str_Name);      //SqlParameter sp2 = new SqlParameter("@Pwd", str_Pwd);      //cmd.Parameters.Add(sp);      //cmd.Parameters.Add(sp2);  #

asp.net中GridView控件遍历的小例子_实用技巧

复制代码 代码如下: int intCount = this.GridView1.Rows.Count; //总行数for (int i = 0; i < intCount; i++){  Label1.Text = ((HyperLink)GridView1.Rows[i].Cells[0].Controls[0]).Text.ToString().Trim(); } for (i = 0; i < GridViewID.Rows.Count; i++){   CheckBox chkVot

ASP.NET中读取XML文件信息的4种方法与示例代码_实用技巧

方法一 :使用XML控件 <% @ Page Language="C#"%> <html> <body>          <h3><font face="Verdana">读取XML方法一</font></h3>        <from runat=server>         <asp:Xml id="xml1" DocumentSour

asp.net中生成缩略图并添加版权实例代码_实用技巧

复制代码 代码如下: //定义image类的对象Drawing.Image image,newimage;//图片路径protected string imagePath;//图片类型protected string imageType;//图片名称protected string imageName;//提供一个回调方法,用于确定Image对象在执行生成缩略图操作时何时提前取消执行//如果此方法确定 GetThumbnailImage 方法应提前停止执行,则返回 true:否则返回 false

asp.net DZ论坛中根据IP地址取得所在地的代码_实用技巧

使用方法: IpSearch.GetAddressWithIP("202.96.128.167")CS类代码 复制代码 代码如下: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI

asp.net中利用ashx实现图片防盗链代码_实用技巧

GET /Img.ashx?img=svn_work.gif HTTP/1.1 Accept: */* Referer: http://www.svnhost.cn/ Accept-Language: zh-cn UA-CPU: x86 Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727

Ext.net中的MessageBox的简单应用实现代码_实用技巧

地址:http://examples.ext.net/ 实例中,使用的方法貌似挺复杂的. 前台.aspx文件中 复制代码 代码如下: <%@ Page Language="C#" %> <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %> <script runat="server">

ASP.NET对IIS中的虚拟目录进行操作的代码_实用技巧

复制代码 代码如下: //假如虚拟目录名为"Webtest",先在项目中引用 //System.DirectoryServices.dll,再 using System.DirectoryServices; protected System.DirectoryServices.DirectoryEntry dirroot; 1.添加新的虚拟目录 复制代码 代码如下: DirectoryEntry newVirDir = dirroot.Children.Add("Webtes