C#中6种常用集合类小结

 更新时间:2024年11月08日 10:24:59   作者:技术拾荒者  
这篇文章主要为大家详细介绍了C#中6种常用集合类,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下

一.先来说说数组的不足

也可以说集合与数组的区别

1.数组是固定大小的,不能伸缩。虽然System.Array.Resize这个泛型方法可以重置数组大小,但是该方法是重新创建新设置大小的数组,用的是旧数组的元素初始化。随后以前的数组就废弃!而集合却是可变长的

2.数组要声明元素的类型,集合类的元素类型却是object.

3.数组可读可写不能声明只读数组。集合类可以提供ReadOnly方法以只读方式使用集合。

4.数组要有整数下标才能访问特定的元素,然而很多时候这样的下标并不是很有用。集合也是数据列表却不使用下标访问。很多时候集合有定制的下标类型,对于队列和栈根本就不支持下标访问!

二.下面讲述6种常用集合

1.ArrayList类

 using  System;
 using  System.Collections.Generic;
 using  System.Text;
 using  System.Collections;
 namespace  ConsoleApplication1
 {
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList al = new ArrayList();
            al.Add(100);//单个添加
            foreach (int number in new int[6] { 9, 3, 7, 2, 4, 8 })
            {
                al.Add(number);//集体添加方法一//清清月儿 http://blog.csdn.net/21aspnet/
            }
            int[] number2 = new int[2] { 11,12 };
            al.AddRange(number2);//集体添加方法二
            al.Remove(3);//移除值为3的
            al.RemoveAt(3);//移除第3个
            ArrayList al2 = new ArrayList(al.GetRange(1, 3));//新ArrayList只取旧ArrayList一部份
 
 
            Console.WriteLine("遍历方法一:");
            foreach (int i in al)//不要强制转换
            {
                Console.WriteLine(i);//遍历方法一
            }
 
            Console.WriteLine("遍历方法二:");
            for (int i = 0; i != al2.Count; i++)//数组是length
            {
                int number = (int)al2[i];//一定要强制转换
                Console.WriteLine(number);//遍历方法二
 
            }
        }
    }
}

2.Stack类

栈,后进先出。push方法入栈,pop方法出栈。

​using  System;
 using  System.Collections.Generic;
 using  System.Text;
 using  System.Collections;
 namespace  ConsoleApplication1
 {
    class Program
    {
        static void Main(string[] args)
        {
            Stack sk = new Stack();
            Stack sk2 = new Stack();
            foreach (int i in new int[4] { 1, 2, 3, 4 })
            {
                sk.Push(i);//填充
                sk2.Push(i);
            }
            
            foreach (int i in sk)
            {
                Console.WriteLine(i);//遍历
            }
 
            sk.Pop();
            Console.WriteLine("Pop");
            foreach (int i in sk)
            {
                Console.WriteLine(i);
            }
            
            sk2.Peek();//弹出最后一项不删除//清清月儿 http://blog.csdn.net/21aspnet/
            Console.WriteLine("Peek");
            foreach (int i in sk2)
            {
                Console.WriteLine(i);
            }
 
            while (sk2.Count != 0)
            {
                int i = (int)sk2.Pop();//清空
                sk2.Pop();//清空
            }
            Console.WriteLine("清空");
            foreach (int i in sk2)
            {
                Console.WriteLine(i);
            }
        }
    }
}

3.Queue类

队列,先进先出。enqueue方法入队列,dequeue方法出队列。

 using  System;
 using  System.Collections.Generic;
 using  System.Text;
 using  System.Collections;
 namespace  ConsoleApplication1
 {
    class Program
    {
        static void Main(string[] args)
        {
            Queue qu = new Queue();
            Queue qu2 = new Queue();
            foreach (int i in new int[4] { 1, 2, 3, 4 })
            {
                qu.Enqueue(i);//填充
                qu2.Enqueue(i);
            }
            
            foreach (int i in qu)
            {
                Console.WriteLine(i);//遍历
            }
 
            qu.Dequeue();
            Console.WriteLine("Dequeue");
            foreach (int i in qu)
            {
                Console.WriteLine(i);
            }
            
            qu2.Peek();//弹出最后一项不删除
            Console.WriteLine("Peek");
            foreach (int i in qu2)
            {
                Console.WriteLine(i);
            }
 
            while (qu2.Count != 0)
            {
                int i = (int)qu2.Dequeue();//清空
                qu2.Dequeue();//清空
            }
            Console.WriteLine("清空");
            foreach (int i in qu2)
            {
                Console.WriteLine(i);
            }
        }
    }
}

4.Hashtable类

哈希表,名-值对。类似于字典(比数组更强大)。哈希表是经过优化的,访问下标的对象先散列过。如果以任意类型键值访问其中元素会快于其他集合。GetHashCode()方法返回一个int型数据,使用这个键的值生成该int型数据。哈希表获取这个值最后返回一个索引,表示带有给定散列的数据项在字典中存储的位置。

 using  System;
 using  System.Collections.Generic;
 using  System.Text;
 using  System.Collections;
 namespace  ConsoleApplication1
 {
    class Program
    {
        public static void Main()
        {
 
            // Creates and initializes a new Hashtable.
            Hashtable myHT = new Hashtable();
            myHT.Add("one", "The");
            myHT.Add("two", "quick");
            myHT.Add("three", "brown");
            myHT.Add("four", "fox");
 
            // Displays the Hashtable.//清清月儿 http://blog.csdn.net/21aspnet/
            Console.WriteLine("The Hashtable contains the following:");
            PrintKeysAndValues(myHT);
        }
 
 
        public static void PrintKeysAndValues(Hashtable myHT)
        {
            foreach (string s in myHT.Keys)
                Console.WriteLine(s);
 
            Console.WriteLine(" -KEY- -VALUE-");
            foreach (DictionaryEntry de in myHT)
                Console.WriteLine(" {0}: {1}", de.Key, de.Value);
            Console.WriteLine();
        }
    }
}

5.SortedList类

与哈希表类似,区别在于SortedList中的Key数组排好序的。

 using  System;
 using  System.Collections.Generic;
 using  System.Text;
 using  System.Collections;
 namespace  ConsoleApplication1
 {
    class Program
    {
        public static void Main()
        {
 
            SortedList sl = new SortedList();
            sl["c"] = 41;
            sl["a"] = 42;
            sl["d"] = 11;
            sl["b"] = 13;
 
            foreach (DictionaryEntry element in sl)
            {
                string s = (string)element.Key;
                int i = (int)element.Value;
                Console.WriteLine("{0},{1}",s,i);
            }
        }
    }
}

6.NameValueCollection类

官方给NameValueCollection定义为特殊集合一类,在System.Collections.Specialized下。

System.Collections.Specialized下还有HybridDicionary类,建议少于10个元素用HybridDicionary,当元素增加会自动转为HashTable。

System.Collections.Specialized下还有HybridDicionary类,字符串集合。

System.Collections.Specialized下还有其他类大家可以各取所需!

言归正转主要说NameValueCollection,HashTable 和 NameValueCollection很类似但是他们还是有区别的,HashTable 的KEY是唯一性,而NameValueCollection则不唯一!

 using  System;
 using  System.Collections.Generic;
 using  System.Collections;
 using  System.Collections.Specialized;
 namespace  ConsoleApplication1
 {
 
    class Program
    {
 
        static void Main(string[] args)
        {
            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            ht.Add("DdpMDisplaySeq".Trim(), "Display Sequence".Trim());
            ht.Add("DdpMNameChi".Trim(), "Name (Chinese)".Trim());
            ht.Add("DdpMNameEng".Trim(), "Name (English)".Trim());
            ht.Add("Comment".Trim(), "Comment".Trim());
            ht.Add("DdpMMarketCode".Trim(), "Market Code".Trim());
            foreach (object key in ht.Keys)
            {
                Console.WriteLine("{0}/{1}    {2},{3}", key, ht[key], key.GetHashCode(), ht[key].GetHashCode());
            }
            Console.WriteLine(" ");//清清月儿 http://blog.csdn.net/21aspnet/
            NameValueCollection myCol = new NameValueCollection();
            myCol.Add("DdpMDisplaySeq".Trim(), "Display Sequence".Trim());
            myCol.Add("DdpMNameChi".Trim(), "Name (Chinese)".Trim());
            myCol.Add("DdpMNameChi".Trim(), "Name (English)".Trim());
            myCol.Add("Comment".Trim(), "Comment".Trim());
            myCol.Add("DdpMMarketCode".Trim(), "Market Code".Trim());
            foreach (string key in myCol.Keys)
            {
                Console.WriteLine("{0}/{1} {2},{3}", key, myCol[key], key.GetHashCode(), myCol[key].GetHashCode());
            }
 
        }
 
    }
 
 
}

到此这篇关于C#中6种常用集合类小结的文章就介绍到这了,更多相关C#常用集合类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C# 绘制实时折线图,波形图

    C# 绘制实时折线图,波形图

    这篇文章主要介绍了C# 绘制实时折线图,波形图的方法,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • C#多线程传递参数及任务用法示例

    C#多线程传递参数及任务用法示例

    这篇文章主要介绍了C#多线程传递参数及任务用法,结合简单实例形式分析了C#多线程的使用及相关的参数传递与任务创建等使用技巧,需要的朋友可以参考下
    2016-06-06
  • Unity 2017使用UGUI实现大转盘抽奖

    Unity 2017使用UGUI实现大转盘抽奖

    这篇文章主要为大家详细介绍了Unity 2017使用UGUI实现大转盘抽奖,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • C#模式画刷HatchBrush用法实例

    C#模式画刷HatchBrush用法实例

    这篇文章主要介绍了C#模式画刷HatchBrush用法,实例分析了模式画刷HatchBrush绘图的相关技巧,需要的朋友可以参考下
    2015-06-06
  • 使用C#代码统计Word文档的单词、字符、段落、行数和页数

    使用C#代码统计Word文档的单词、字符、段落、行数和页数

    对单词、字符、段落、行数和页数进行准确统计,对于实现精确的文档分析至关重要,通过细致地追踪这些指标,作者可以更深入地了解文档的长度、结构和整体构成,在本文中,我们将介绍如何使用 C# 代码统计 Word 文档中的单词、字符、段落、行数和页数,需要的朋友可以参考下
    2025-12-12
  • unity绘制一条流动的弧线(贝塞尔线)

    unity绘制一条流动的弧线(贝塞尔线)

    这篇文章主要为大家详细介绍了unity绘制一条流动弧线的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-06-06
  • C# 文件操作函数 创建文件 判断存在

    C# 文件操作函数 创建文件 判断存在

    本文列举了C#中文件操作中常用的函数,创建文件和判断文件存不存在的基本使用,简单实用,希望能帮到大家。
    2016-05-05
  • C#定时器Timer实现精确到1-2毫秒以内

    C#定时器Timer实现精确到1-2毫秒以内

    最近在排查项目OTA的一个问题,触发了一毫秒或者2毫秒执行一次进程间通信的,导致通信阻塞的问题,这样就需要用到模拟触发1ms或者2ms触发事件,所以本文给大家介绍了C# 定时器 Timer 如何精确到 1-2 毫秒以内,需要的朋友可以参考下
    2024-12-12
  • C#简单了解接口(Interface)使用方法

    C#简单了解接口(Interface)使用方法

    这篇文章主要介绍了C#简单了解接口(Interface)使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • 基于C# winform实现图片上传功能的方法

    基于C# winform实现图片上传功能的方法

    这篇文章主要介绍了基于C# winform实现图片上传功能的方法,很实用的功能,需要的朋友可以参考下
    2014-07-07

最新评论