這個問題也是別人問到的,下面貼出來供大家參考。
C# 代碼
public class Person
{
public String Name { set; get; }
public int Age { set; get; }
public String Url { set; get; }
}
protected void Page_Load(object sender, EventArgs e)
{
//Dictionary 的簡寫形式
Dictionary<int, String> dict = new Dictionary<int, string>() { { 1, "A" }, { 2, "BB" } };
foreach (KeyValuePair<int, String> d in dict)
{
Response.Write("<li>" + d.Key + " = " + d.Value);
}
//Hashtable 的簡寫形式www.2cto.com
Hashtable myHash = new Hashtable() { { "key1", "AA" }, { "key2", "BB" } };
foreach (DictionaryEntry d in myHash)
{
Response.Write("<li>" + d.Key + " = " + d.Value);
}
ArrayList ar = new ArrayList() { "A", "B", "C" };
foreach (string d in ar)
{
Response.Write("<li>" + d);
}
//創建普通對象
Person p = new Person() { Name = "net_lover", Age = 28, Url = "http://dotnet.aspx.cc" };
}
作者 孟憲會