随笔 - 2146  文章 - 19 评论 - 11846 阅读 - 1267万


System.Collections.Generic.Dictionary<>;       //键/值对集合
System.Collections.Generic.KeyValuePair<>;     //键/值对结构, 作为 Dictionary<> 的一个元素存在
System.Collections.Generic.SortedDictionary<>; //相当于 Key 能自动排序 Dictionary<>
System.Collections.Generic.SortedList<>;       //和 SortedDictionary<> 功能相似, 但内部算法不同, 其 Keys、Values 可通过索引访问

System.Collections.Generic.HashSet<>;   //无序、无重复的元素集合
System.Collections.Generic.SortedSet<>; //相当于能自动排序的 HashSet<>
System.Collections.Generic.List<>;      //相当于泛型的 ArrayList, 元素可重复、可排序、可插入、可索引访问

System.Collections.Generic.Queue<>; //队列, 先进先出
System.Collections.Generic.Stack<>; //堆栈, 后进先出

System.Collections.Generic.LinkedList<>;     //双向链表
System.Collections.Generic.LinkedListNode<>; //LinkedList<> 的节点

System.Collections.Generic.SynchronizedCollection<>;         //线程安全的集合
System.Collections.Generic.SynchronizedReadOnlyCollection<>; //线程安全的只读集合
System.Collections.Generic.SynchronizedKeyedCollection<>;    //线程安全的键/值集合


Dictionary<>、KeyValuePair<>:
protected void Button1_Click(object sender, EventArgs e)
{
    Dictionary<string, int> dict = new Dictionary<string, int>();
    dict.Add("K1", 123);
    dict["K2"] = 456;
    dict.Add("K3", 789);

    string str = "";
    foreach (KeyValuePair<string, int> k in dict)
    {
        str += string.Format("{0}-{1}; ", k.Key, k.Value); //K1-123; K2-456; K3-789; 
    }
    TextBox1.Text = str;
}


SortedDictionary<>:
protected void Button1_Click(object sender, EventArgs e)
{
    SortedDictionary<string, int> dict = new SortedDictionary<string, int>();
    dict.Add("K3", 333);
    dict["K1"] = 111;
    dict.Add("K2", 222);

    SortedDictionary<string, int>.KeyCollection ks = dict.Keys;
    SortedDictionary<string, int>.ValueCollection vs = dict.Values;

    string s1, s2, s3;
    s1 = s2 = s3 = "";

    foreach (KeyValuePair<string, int> k in dict)
    {
        s1 += string.Format("{0}-{1}; ", k.Key, k.Value); //K1-111; K2-222; K3-333;
    }

    foreach (string s in ks) { s2 += s + "; "; }          //K1; K2; K3;
    foreach (int n in vs) { s3 += n.ToString() + "; "; }  //111; 222; 333; 

    TextBox1.Text = s1 + "\n" + s2 + "\n" + s3;
}


SortedList<>:
protected void Button1_Click(object sender, EventArgs e)
{
    SortedList<string, int> dict = new SortedList<string, int>();
    dict.Add("K3", 333);
    dict["K1"] = 111;
    dict.Add("K2", 222);

    string s1, s2, s3;
    s1 = s2 = s3 = "";

    foreach (KeyValuePair<string, int> k in dict)
    {
        s1 += string.Format("{0}-{1}; ", k.Key, k.Value); //K1-111; K2-222; K3-333;
    }

    s2 = dict.Keys[0];              //K1
    s3 = dict.Values[0].ToString(); //111

    TextBox1.Text = s1 + "\n" + s2 + "\n" + s3;
}


HashSet<>、SortedSet<>:
protected void Button1_Click(object sender, EventArgs e)
{
    HashSet<string> hs = new HashSet<string>();
    hs.Add("ccc");
    hs.Add("bbb");
    hs.Add("aaa");

    SortedSet<string> ss = new SortedSet<string>();
    ss.Add("ccc");
    ss.Add("bbb");
    ss.Add("aaa");

    string s1 = "", s2 = "";

    foreach (string s in hs) { s1 += s + " "; } //ccc bbb aaa 
    foreach (string s in ss) { s2 += s + " "; } //aaa bbb ccc 

    TextBox1.Text = s1 + "\n" + s2;
}


List<>:
protected void Button1_Click(object sender, EventArgs e)
{
    List<int> list = new List<int>();
    list.Add(11);
    list.Add(22);
    list.Insert(0, 33);

    string s1, s2 = "", s3, s4 = "";

    s1 = list[0].ToString(); //33
    for (int i = 0; i < list.Count; i++) { s2 += list[i].ToString() + " "; } //33 11 22

    list.Sort();

    s3 = list[0].ToString(); //11
    foreach (int n in list) { s4 += n.ToString() + " "; } //11 22 33 

    TextBox1.Text = s1 + "\n" + s2 + "\n" + s3 + "\n" + s4;
}


LinkedList<>、LinkedListNode<>:
protected void Button1_Click(object sender, EventArgs e)
{
    LinkedList<string> list = new LinkedList<string>();
    list.AddFirst("aaa");
    list.AddLast("bbb");
    list.AddFirst("ccc");
    list.AddAfter(list.First, "ddd");
    list.AddBefore(list.Last, "eee");

    string s1 = "", s2 = "", s3 = "", s4 = "", s5 = "";

    foreach (string s in list) { s1 += s + " "; } //ccc ddd aaa eee bbb 

    LinkedListNode<string> node = list.First;
    s2 = node.Value.ToString();         //ccc
    node = node.Next;
    s3 = node.Value.ToString();         //ddd
    node = list.Last.Previous.Previous;
    s4 = node.Value.ToString();         //aaa

    list.Remove("eee");
    list.RemoveFirst();
    list.RemoveLast();

    node = list.First;
    while (node != null)
    {
        s5 += node.Value.ToString() + " "; //ddd aaa 
        node = node.Next;
    }
    TextBox1.Text = s1 + "\n" + s2 + "\n" + s3 + "\n" + s4 + "\n" + s5;
}

posted on   万一  阅读(2147)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
历史上的今天:
2009-01-11 类型转换出现在赋值运算符左边的情况
2008-01-11 关于类的入门例子(7): 遍历窗体的所有父类
2008-01-11 理解 Delphi 的类(七) - 认识类的多态
2008-01-11 理解 Delphi 的类(六) - 认识类的封装
2008-01-11 理解 Delphi 的类(五) - 认识类的继承
2008-01-11 理解 Delphi 的类(四) - 初识类的事件
2008-01-11 理解 Delphi 的类(三) - 初识类的属性


点击右上角即可分享
微信分享提示