• (原创)C#零基础学习笔记008-C#集合处理


    微信公众号已开通,搜索微信公众号:程序喵星人。点击关注^_^

    8.C#集合处理

      1.哈希表集合

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

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

        动态存放键/值对。

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

    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);
                }
            }
        }
    }
    

      

      2.泛型的应用

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

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

      3.字典集合

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

    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的顺序
                }
            }
        }
    }
    

      

  • 相关阅读:
    AppScan安全问题解决方案
    WPF方法 获取控件在屏幕的位置和自动控件位置
    WPF获取鼠标点击位置和API使用
    system.windows.controls
    .net 获取项目的根目录
    InvokeRequired
    控件取图片和矩形框 小测试
    mariadb 设置远程访问
    代码生成器 链接
    分享个网站
  • 原文地址:https://www.cnblogs.com/wodehao0808/p/14273391.html
Copyright © 2020-2023  润新知