Dictionary SortedList HashSet List用法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
#region Dictionary
var dic = new Dictionary<string, string>();
dic.Add("1", "111");
dic.Add("2", "222");
dic.Add("3", "333");
dic.Add("4", "444");
foreach (KeyValuePair<string, string> key in dic)
{
Response.Write(String.Format("这是Dictionary value:{0}<br/>", key.Value));
Response.Write(String.Format("这是Dictionary key:{0}<br/>", key.Key));
}
#endregion
#region SortedList
var sorted = new SortedList<string, string>();
sorted.Add("1", "111");
sorted.Add("2", "222");
sorted.Add("3", "333");
sorted.Add("4", "444");
sorted.Add("5", "555");
foreach (KeyValuePair<string, string> book in sorted)
{
Response.Write(String.Format("这是SortedList values:{0}<br/>", book.Value));
Response.Write(String.Format("这是SortedList key:{0}<br/>", book.Key));
}
foreach (string str in sorted.Values)
{
Response.Write(String.Format("这是SortedList values:{0}<br/>", str));
}
foreach (string str in sorted.Keys)
{
Response.Write(String.Format("这是SortedList key:{0}<br/>", str));
}
if (sorted.ContainsKey("1"))
{
Response.Write("SortedList 列表以存在该值1<br/>");
}
string isnum;
if (sorted.TryGetValue("1", out isnum))
{
Response.Write(String.Format("SortedList 列表以存在该值{0}<br/>", isnum));
}
#endregion
var hashset = new HashSet<string>();
hashset.Add("1");
if (hashset.Add("1"))
hashset.Add("1");
if (hashset.Add("2"))
hashset.Add("2");
foreach (var hash in hashset)
{
Response.Write(String.Format("这是HashSet{0}<br/>", hash));
}
var sortedset = new SortedSet<string>();
sortedset.Add("1");
if (sortedset.Add("1"))
sortedset.Add("1");
if (sortedset.Add("2"))
sortedset.Add("2");
foreach (var _sorted in sortedset)
{
Response.Write(String.Format("这是SortedSet{0}<br/>", _sorted));
}
List<string> list=new List<string>;
list.add("1");
list.add("2");
list.add("3");
foreach (string str in list)
{
Response.Write(String.Format("这是List{0}<br/>", str ));
}
}
}
这是简单用法