學用 ASP.Net 之 System.Collections.Hashtable 類與 DictionaryEntry 結構

來源:互聯網
上載者:User
DictionaryEntry 是包含 Key / Value 一對值的簡單結構;

Hashtable(雜湊表)是一組 Key / Value 的集合, 準確地講是一組 DictionaryEntry 的集合.

DictionaryEntry 簡例:
protected void Button1_Click(object sender, EventArgs e){    DictionaryEntry d1;    d1.Key = "k1";    d1.Value = 123;    DictionaryEntry d2 = new DictionaryEntry("k2", 456);    TextBox1.Text = string.Concat(d1.Key, ":", d1.Value, "; ", d2.Key, ":", d2.Value); //k1:123; k2:456}
Hashtable 成員:
/* 靜態方法 */Hashtable.Synchronized(); ///* 屬性 */Count          //元素數IsFixedSize    //IsReadOnly     //IsSynchronized //Keys           //鍵集合SyncRoot       //Values         //值集合/* 方法 */Add()               //添加Clear()             //清空Clone()             //Contains()          //是否包含指定 Key, 同 ContainsKey()ContainsKey()       //是否包含指定 KeyContainsValue()     //是否包含指定 ValueCopyTo()            //複製到Equals()            //GetEnumerator()     //GetObjectData()     //OnDeserialization() //Remove()            //移除/* 擴充方法 */AsParallel()  //AsQueryable() //Cast()      //OfType()    //
入手練習:
protected void Button1_Click(object sender, EventArgs e){    Hashtable hash = new Hashtable();    hash.Add("k1", "AAAAA");    hash.Add("k2", "BBBBB");    hash.Add("k3", "CCCCC");    hash.Add("k4", "DDDDD");    int n1 = hash.Count; //4    string s1 = hash["k2"].ToString(); //BBBBB    hash["k2"] = "12345";    string s2 = hash["k2"].ToString(); //12345    hash.Remove("k2");    int n2 = hash.Count; //3    hash.Clear();    int n3 = hash.Count; //0    TextBox1.Text = string.Concat(n1, "\n", s1, "\n", s2, "\n", n2, "\n", n3);}
遍曆:
protected void Button1_Click(object sender, EventArgs e){    Hashtable hash = new Hashtable();    hash.Add(1, "AAAAA");    hash.Add(2, "BBBBB");    hash.Add(3, "CCCCC");    hash.Add(4, "DDDDD");    string str = "";    foreach (DictionaryEntry de in hash)    {        str += string.Format("{0} : {1}\n", de.Key, de.Value);    }    TextBox1.Text = str;}/*遍曆結果:4 : DDDDD3 : CCCCC2 : BBBBB1 : AAAAA**********/
Contains()、ContainsKey()、ContainsValue():
protected void Button1_Click(object sender, EventArgs e){    Hashtable hash = new Hashtable();    hash.Add("k1", 123);    hash.Add("k2", "ABC");    hash.Add("k3", 3.14);    hash.Add("k4", null);    bool b1 = hash.Contains("k1"); //True    bool b2 = hash.Contains("k4"); //True    bool b3 = hash.Contains("k5"); //False    bool b4 = hash.ContainsKey("k1"); //True    bool b5 = hash.ContainsKey("k4"); //True    bool b6 = hash.ContainsKey("k5"); //False    bool b7 = hash.ContainsValue("3.14"); //False    bool b8 = hash.ContainsValue(3.14);   //True    bool b9 = hash.ContainsValue(null);   //True    bool b0 = hash.ContainsValue("");     //False    TextBox1.Text = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n{7}\n{8}\n{9}",             b1, b2, b3, b4, b5, b6, b7, b8, b9, b0        );}protected void Button2_Click(object sender, EventArgs e){    Hashtable hash = new Hashtable();    if (!hash.Contains("k1"))    {        hash.Add("k1", DateTime.Now);    }    if (hash.Contains("k1"))    {        hash.Remove("k1");    }}
Keys、Values:
protected void Button1_Click(object sender, EventArgs e){    Hashtable hash = new Hashtable();    hash.Add("k1", "AAA");    hash.Add("k2", "BBB");    hash.Add("k3", "CCC");    hash.Add("k4", "DDD");    ICollection ks = hash.Keys;    ICollection vs = hash.Values;    string s1 = "", s2 = "";    foreach (string k in ks) { s1 += k + "; "; } //k4; k1; k2; k3;     foreach (string v in vs) { s2 += v + "; "; } //DDD; AAA; BBB; CCC;    TextBox1.Text = s1 + "\n" + s2;}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.