【原创】C#零基础学习笔记008-C#集合处理

其他路径:

CSDN: https://blog.csdn.net/wodehao0808

微信公众号:程序喵星人

更多资源和视频教程,QQ:1902686547

8 C#集合的处理

8.1 哈希表集合

    Hashtable集合是键/值对的集合。

  DictionaryEntry类型的实例,DictionaryEntry类型有一个key和value属性来读取和设置键和值。

  动态存放键/值对。

  键值是唯一的,不可重复。

8.2 Example: 集合处理:Hashtable表

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Collections;  // 引入 hash 所在的命名空间

// 集合处理:Hashtable表

namespace Lesson_35_1

{

    class Program

    {

        static void Main(string[] args)

        {

            Hashtable hash = new Hashtable();

            // 往hash里添加数据

            hash.Add( 1, "hello" );

            hash.Add( 2, "world" );

            hash.Add( 3, "C#" );

            // 访问hash里的数据,通过hash访问的时候,我们采用的是键这种方式访问

            Console.WriteLine( hash[1] );

            // 遍历方式1:可以采用遍历它的键集合

            var keys = hash.Keys;  // hash键的集合

            foreach (object key in keys)

            {

                Console.WriteLine("键:{0},值:{1}", key, hash[key]);  // 输出顺序跟Add顺序相反

            }

            // 遍历方式2:使用遍历器(迭代器)

            var ie = hash.GetEnumerator();  // 获取一个遍历器

            while (ie.MoveNext())  // 依次遍历集合中的数据,类似于游标

            {

                Console.WriteLine("遍历器--键:{0},值:{1}", ie.Key, ie.Value);

            }

        }

    }

}

8.3 泛型的应用

  泛型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性。

  在泛型类型或泛型方法的定义中,类型参数是一个占位符(placeholder),通常为一个大写字母。

8.4 字典集合

  键值对的存储,这种键值对的存储可以使用泛型。即,字典。

8.5 Example: 集合:泛型应用和字典集合

using System;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Collections;

using System.Collections.Generic;  // 如果要使用泛型,必须引入这个命名空间

// 集合:泛型应用和字典集合

namespace Lesson_36_1

{

    class Program

    {

        static void Main(string[] args)

        {

            // 数据类型不安全:即数据的类型是不确定的。

            // 在使用ArrayList无法保证类型的一致性,例如:

            ArrayList arrList = new ArrayList();

            arrList.Add(1);  // int类型

            arrList.Add("Hello");  // string 类型

            arrList.Add(true);  // bool 类型

            // 以上,arrList的类型是不确定的,不是固定的,所以数据类型不安全。

            // 泛型的特点就是类型安全,类型可以指定为固定类型。

            // 泛型规定了在集合中所存储的数据的类型。

            List<int> list = new List<int>();  // 指定list的类型是int

            list.Add(1);

            list.Add(2);

            list.Add(3);

            // 泛型遍历

            foreach (int i in list )

            {

                Console.WriteLine("泛型foreach遍历,输出的数据:" + i);  // 输出的顺序是add的顺序

            }

            for (int i = 0; i < list.Count; ++i )

            {

                Console.WriteLine("泛型for遍历,输出的数据:" + list[i]);  // 输出的顺序是add的顺序

            }

            // 字典集合的存储

            Dictionary<string, string> dic = new Dictionary<string, string>();  // 指定了 键的类型为string 和 值的类型为string

            dic.Add("1001", "张三");

            dic.Add("1002", "李四");

            dic.Add("1003", "王五");

            // 采用类似于访问hash表的方式去访问字典集合的数据

            var keys = dic.Keys;

            foreach (string key in keys )

            {

                Console.WriteLine("字典采用foreach遍历keys方法输出,键:{0},值:{1}", key, dic[key]);  // 输出的顺序就是add的顺序

            }

            var ie = dic.GetEnumerator();

            while (ie.MoveNext())

            {

                // 注意,这里 ie.Current 后,再 .key 和 .Value。

                Console.WriteLine("字典采用遍历器GetEnumerator方法输出,键:{0},值:{1}", ie.Current.Key, ie.Current.Value);  // 输出的顺序就是add的顺序

            }

        }

    }

}