C#常用集合的使用

大多数集合都在System.Collections,System.Collections.Generic两个命名空间。其中System.Collections.Generic专门用于泛型集合。

针对特定类型的集合类型位于System.Collections.Specialized;命名空间;

线程安全的集合类位于System.Collections.Concurrent;命名空间。

下面是集合和列表实现的接口如下:

 

一、列表

    [Serializable]
    [DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]
    [DebuggerDisplay("Count = {Count}")]
    public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable


从这个可以看出,泛型集合List<T>实现了这么多接口,具体接口的信息可以通过工具查看。

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            List<String> list = new List<string>();
            list.Add("张三");
            list.Add("李四");
            list.Add("王五");
            list.Add("田六");
            list.Add("赵七");

            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine("for循环:" + i.ToString() + "=" + list[i]);
            }

            list.RemoveAt(0);
            foreach (String item in list)
            {
                Console.WriteLine("foreach迭代:" + item);
            }
            list.AddRange(new String[] { "Hello1", "Hello2", "Hello3" });

            list.ForEach(Print);

            Console.Read();
        }

        private static void Print(String item)
        {
            Console.WriteLine("ForEach:" + item);
        }
    }

}


 二、队列

队列先进先出,一头进一头出,用Queue<T>实现

    [Serializable]
    [DebuggerTypeProxy(typeof(System_QueueDebugView<>))]
    [ComVisible(false)]
    [DebuggerDisplay("Count = {Count}")]
    public class Queue<T> : IEnumerable<T>, ICollection, IEnumerable

可以看出队列实现了集合的接口,迭代的接口


using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            Queue<String> queue = new Queue<string>();
            //进队
            queue.Enqueue("张三");
            queue.Enqueue("李四");
            queue.Enqueue("王五");
            queue.Enqueue("田六");
            queue.Enqueue("赵七");

            foreach (String item in queue)
            {
                Console.WriteLine("foreach迭代:" + item);
            }

            //出队
            while (queue.Count > 0)
            {
                Console.WriteLine("出队:" + queue.Dequeue());
            }

            Console.Read();
        }
    }
}


 三、栈

栈:从同一边先进后出,用Stack<T>实现

 [DebuggerDisplay("Count = {Count}")]
    [DebuggerTypeProxy(typeof(System_StackDebugView<>))]
    [ComVisible(false)]
    public class Stack<T> : IEnumerable<T>, ICollection, IEnumerable


栈也是实现了集合接口与迭代接口的

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            Stack<String> stack = new Stack<string>();
            //进栈
            stack.Push("张三");
            stack.Push("李四");
            stack.Push("王五");
            stack.Push("田六");
            stack.Push("赵七");

            foreach (String item in stack)
            {
                Console.WriteLine("foreach迭代:" + item);
            }

            //出栈
            while (stack.Count > 0)
            {
                Console.WriteLine("出栈:" + stack.Pop());
            }

            Console.Read();
        }
    }
}


 四、链表

LinkedList是一个双向链表,链表有个有点,就是在链表中间插入、删除元素很快,但是查找中间与末尾的元素很慢,需要一个节点一个节点的去找。

    [Serializable]
    [DebuggerTypeProxy(typeof(System_CollectionDebugView<>))]
    [DebuggerDisplay("Count = {Count}")]
    [ComVisible(false)]
    public class LinkedList<T> : ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, ISerializable, IDeserializationCallback

由此可见链表也是有集合的特性的,可以迭代,同时还有链表的特性


using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            LinkedList<String> lList = new LinkedList<string>();
            LinkedListNode<String> node = new LinkedListNode<string>("root");
            lList.AddFirst(node);
            node = lList.AddAfter(node, "张三");
            node = lList.AddAfter(node, "李四");
            node = lList.AddAfter(node, "王五");
            node = lList.AddAfter(node, "田六");
            node = lList.AddAfter(node, "赵七");

            foreach (String item in lList)
            {
                Console.WriteLine("foreach迭代:" + item);
            }

            node = lList.First;
            Console.WriteLine("第一个元素:" + node.Value);
            node = lList.Last;
            Console.WriteLine("最后一个元素:" + node.Value);
            Console.Read();
        }
    }
}


 五、有序列表

SortedList采用键-值对存储,键不能重复,并且会根据key进行排序

    [Serializable]
    [DebuggerTypeProxy(typeof(System_DictionaryDebugView<,>))]
    [DebuggerDisplay("Count = {Count}")]
    [ComVisible(false)]
    public class SortedList<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable

可以看出SortedList不仅具有字典的特性,还有集合,迭代的功能


using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            //Key必须唯一,如果不唯一可以考虑Lookup<TKey,TElement>
            SortedList<int, String> sList = new SortedList<int, string>();
            sList.Add(100, "张三");
            sList.Add(21, "李四");
            sList.Add(13, "王五");
            sList.Add(44, "田六");
            sList.Add(35, "赵七");

            foreach (KeyValuePair<int, String> item in sList)
            {
                Console.WriteLine("key=" + item.Key.ToString() + ";value=" + item.Value);
            }

            Console.Read();
        }
    }
}


 六、字典

字典是很复杂的数据结构,允许通过key来查找值,字典可以自由添加、删除元素,没有集合由于移动元素导致的开销。

[Serializable]
    [DebuggerTypeProxy(typeof(Mscorlib_DictionaryDebugView<,>))]
    [DebuggerDisplay("Count = {Count}")]
    [ComVisible(false)]
    public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback

可以看出字典也具有集合的特性,可以迭代


using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            //Key必须唯一
            Dictionary<int, String> dict = new Dictionary<int, string>();
            dict.Add(11, "张三");
            dict.Add(1, "李四");
            dict.Add(2, "王五");
            dict.Add(16, "田六");
            dict.Add(12, "赵七");

            foreach (KeyValuePair<int, String> item in dict)
            {
                Console.WriteLine("key=" + item.Key.ToString() + ";value=" + item.Value);
            }

            Console.Read();
        }
    }
}


 说到字典,顺便谈一下有序字典,与有序列表对应;SortedDictionary,SortedList,SortedSet

会根据Key进行排序

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            //Key必须唯一
            SortedDictionary<int, String> dict = new SortedDictionary<int, string>();
            dict.Add(11, "张三");
            dict.Add(1, "李四");
            dict.Add(2, "王五");
            dict.Add(16, "田六");
            dict.Add(12, "赵七");

            foreach (KeyValuePair<int, String> item in dict)
            {
                Console.WriteLine("key=" + item.Key.ToString() + ";value=" + item.Value);
            }

            Console.Read();
        }
    }
}

 

 七、集

集(Set):包含不重复元素,常用HashSet,SortedSet

 [Serializable]
    [DebuggerDisplay("Count = {Count}")]
    [DebuggerTypeProxy(typeof(HashSetDebugView<>))]
    public class HashSet<T> : ISerializable, IDeserializationCallback, ISet<T>, ICollection<T>, IEnumerable<T>, IEnumerable

 [Serializable]
    [DebuggerTypeProxy(typeof(SortedSetDebugView<>))]
    [DebuggerDisplay("Count = {Count}")]
    public class SortedSet<T> : ISet<T>, ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, ISerializable, IDeserializationCallback


 

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            HashSet<String> hSet = new HashSet<string>();
            hSet.Add("张三");
            hSet.Add("李四");
            hSet.Add("王五");
            hSet.Add("田六");
            hSet.Add("赵七");

            foreach (String item in hSet)
            {
                Console.WriteLine("foreach迭代:" + item);
            }

            Console.Read();
        }
    }
}


using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            SortedSet<String> hSet = new SortedSet<string>();
            hSet.Add("张三");
            hSet.Add("李四");
            hSet.Add("王五");
            hSet.Add("田六");
            hSet.Add("赵七");

            foreach (String item in hSet)
            {
                Console.WriteLine("foreach迭代:" + item);
            }

            Console.Read();
        }
    }
}


性能比较:

 

---------------------------------------------------------------------------------------------------------------------------

  • 23
    点赞
  • 124
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
课程通过实际项目融入常用开发技术架构,讲授风格独特,提供详细上课日志及答疑,赠送配套的项目架构源码注释详细清晰且表达通俗,均能直接在实际项目中应用,正真的物超所值,价格实惠任务作业:综合运用《C#/.Net企业级系统架构设计实战精讲教程》课程所学知识技能设计一个学生成绩管理系统的架构。要求:1.系统基于MVC的三层架构,各层单独建不同的解决方案文件夹。2.采用Model First开发方式,设计架构时只需要设计学生表(TbStudent)和课程表(TbCourse)。学生表必须有的字段是ID、stuName、age;课程表必须有的字段是ID、courseName、content。3.数据访问层采用Entity Framework或NHibernate来实现,必须封装对上述表的增删改查方法。4.必须依赖接口编程,也就是必须要有数据访问层的接口层、业务逻辑层的接口层等接口层。层层之间必须减少依赖,可以通过简单工厂或抽象工厂。5.至少采用简单工厂、抽象工厂、Spring.Net等技术中的2种来减少层与层之间的依赖等。6.封装出DbSession类,让它拥有所有Dal层实例和SaveChanges方法。7.设计出数据访问层及业务逻辑层主要类的T4模板,以便实体增加时自动生成相应的类。8.表现层要设计相关的控制器和视图来验证设计的系统架构代码的正确性,必须含有验证增删改查的方法。9.开发平台一定要是Visual Studio平台,采用C#开发语言,数据库为SQL Server。10.提交整个系统架构的源文件及生成的数据库文件。(注意: 作业需写在CSDN博客中,请把作业链接贴在评论区,老师会定期逐个批改~~)
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值