asp.Netコレクションクラス
2417 ワード
1. IEnumerable
protected void Page_Load(object sender, EventArgs e)
{
IEnumerable ie = AllGet();
Response.Write(new JavaScriptSerializer().Serialize(ie));
}
public IEnumerable AllGet()
{
return new string[] { "Item1", "Item2" }.Select(s => new
{
Name = s,
Code = s,
Items = new ArrayList
{
new { Name = "Item1" },
new { Name = "Item2" },
new { Name = "Item3" },
new { Name = "Item4" },
}
});
}
:
[{"Name":"Item1","Code":"Item1","Items":[{"Name":"Item1"},{"Name":"Item2"},{"Name":"Item3"},{"Name":"Item4"}]},
{"Name":"Item2","Code":"Item2","Items":[{"Name":"Item1"},{"Name":"Item2"},{"Name":"Item3"},{"Name":"Item4"}]}]
2. List
/// <summary>
/// ["1","a","0.5"]
/// </summary>
/// <returns> List </returns>
public List<string> TestAjaxMethod(int a, string b, double c)
{
return new List<string> { a.ToString(), b, c.ToString() };
}
3.Dictionary
private Dictionary<int, string> dic = new Dictionary<int, string>();
4.NameValueCollection
:using System.Collections.Specialized;
,
NameValueCollection col = new NameValueCollection();
col.Add("red", "rojo");// red rojo,rouge
// ,
PrintKeysAndValues(col);
//
col[1];// 1
col["red"]);// red rojo
// red
col.Remove("red");
for (int i = 0; i < col.Count; i++)
//
col.Clear();
// , ,
col.GetKey(i)
col.Get(i)
foreach (string key in col.Keys)
col[key]
5.Hashtable
Hashtable NameValueCollection ;
HashTable - , .
Hashtable ht = new Hashtable();
ht.Add("key","value");