学用 ASP.Net 之 System.Collections.Hashtable 类与 DictionaryEntry 结构


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()       //是否包含指定 Key
ContainsValue()     //是否包含指定 Value
CopyTo()            //复制到
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 : DDDDD
3 : CCCCC
2 : BBBBB
1 : 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;
}

转载于:https://my.oschina.net/hermer/blog/320696

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值