• C#中泛型学习笔记


    前言:这篇博客我主要说一下C#中泛型的使用,也就是List和Dictionary字典集合的使用,我在这里说的主要说的是如何去使用,而不是长篇大论的去研究泛型的底层,但我们有一定程序的时候在研究,使学习的能够很快的学习集合然后自己研究集合的一些高级用法,不在最后还列举出了一些常用的小案例。

    1. 泛型集合

    (1) 泛型集合就是不确定的集合,语法中有一个尖括号,里面放什么类型,这个集合就变成什么类型

    (2)List

                1)举例说明:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    static void Main(string[] args)
     
    {
     
            List<int> listInt = new List<int>();
     
            listInt.AddRange(new int[] { 1, 34, 54, 65, 76, 78 });
     
            int sum = 0;
     
            for (int i = 0; i < listInt.Count; i++)
      
             {
     
                     sum += listInt[i];
     
             }
     
             Console.WriteLine(sum);
     
             Console.ReadKey();
     
    }

         (3)Dictionary  (Dictionary<TKey,TValue>)

                定义一个泛型集合:Dictionary<TKey,Tvalue> dic=new Dictionary<TKey,Tvalue>();

                1)增加

                       Add 将指定的键值对添加到字典集合中

                       方法原型:void dic.Add(T key,T Value)

                              //输出结果是添加失败,请检查,以为添加了相同的键

                2)删除

                       Remove 从字典集合中移除指定的键的值

                              方法原型:bool dic.Remove(TKey key);                     

                3)查询

                       ContainsKey 得到字典集合中是否包含指定的键

                              方法原型:bool dic.ContainsKey(TKey,key);     

                                     //输出结果:已经存在

                       COntainsValue 得到字典集合中是否包含指定的值

                              方法原型:bool dic.ContainsValue(TValue,value);

                                     Dictionary<string, string> openWith = new Dictionary<string, string>();

                                     openWith.Add("txt", "notepad.exe");

                                     openWith.Add("bmp", "paint.exe");

                                     openWith.Add("dib", "paint.exe");

                                     openWith.Add("rtf", "wordpad.exe");

                                     if (openWith.ContainsValue("paint.exe"))

                                     {

                                            Console.WriteLine("已经存在");

                                     }

                                     //输出结果:已经存在

                4)TryGetValue 获得于指定的键相关联的值

                       方法原型:bool dic.TryGetValue(TKey key,out TVlaue value);

                              //输出结果:key=rtf,value=wordpad.exe

                       1)举例说明:

                              static void Main(string[] args)

                              {

                                     Dictionary<char, string> dic = new Dictionary<char, string>();

                                     dic.Add('1', "爱情这东西");

                                     foreach (KeyValuePair<char, string> item in dic)

                                     {

                                            Console.WriteLine(item);

                                     }

                                     Console.ReadKey();

                              }

         (4)案例1:把分拣奇数的程序用泛型实现

         (5)案例2:将int数组中的奇数放到一个新的int数组中返回

         (6)案例3:从一个整数的List<int>中取出最大数

         (7)把123转换为"壹贰叁"

         (8)计算字符串中每种字符出现的次数

    1. Dictionary就是Hashtable的泛型形式

    (1) 哈尔算法是一个函数

                Add(Key,Value);

                dic[Key];

    (2)哈希算法是一个通过Key来计算地址的函数

                1)传入一个key和一个value后

                2)通过哈希算法计算key的到一个地址

                3)将地址存入键值对集合,并将value存入地址所在的地方

                4)等到访问的时候直接通过key计算出地址,直接找到存储的变量

    1. 能不能用for循环遍历一个集合Dic

    (1) 在for循环中如果不使用对应的递增序号,"我"就认为不叫使用了for循环

         (2)foreach循环的过程

                1)找到数据源,调用GetEnumertor方法,得到枚举值

                2)in,调用MoveNext方法

                3)如果MoveNext返回true,使用Current得到当前数据

                4)如果返回false,则跳出循环

                       static void Main(string[] args)

                       {

                              Dictionary<string, string> dic = new Dictionary<string, string>();

                              dic.Add("1111", "2222");

                              dic.Add("0000", "3333");

                              var enumrator = dic.GetEnumerator();

                              //while (enumrator.MoveNext())

                              //{

                              //    Console.WriteLine(enumrator.Current.Key + "," + enumrator.Current.Value);

                              //}

                              for (; enumrator.MoveNext(); )

                              {

                                     Console.WriteLine(enumrator.Current.Key + "," + enumrator.Current.Value);

                              }

                       }

    1. 等于

    (1) Equals 确定指定的Object是否等于当前的Object类型

                方法原型:

                       bool Equals(Object obj)

                       Object Obj1 = new Object();

                Object Obj2 = new Object();

                Console.WriteLine(Obj1.Equals(Obj2));

                Obj2 = Obj1;

                Console.WriteLine(Obj1.Equals(Obj2));

                       输出结果: False,True

    作者:韩迎龙
    出处:http://www.cnblogs.com/hanyinglong
    QQ群:159227188
    本页版权归作者和博客园所有,欢迎转载,但未经作者同意必须保留此段声明, 且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利

    分类: C#
    标签: C#泛型ListDictionary
  • 相关阅读:
    js json 与字符串 转换过程由于书写不统一规范引发的一个问题
    asp.net 运行时,"未能映射路径"
    android html 图片处理类--加载富文本工具类
    System.Web.HttpCompileException (0x80004005): (0): error CS0016: 未能写入输出文件
    “请运行Chkdsk工具”怎么解决
    android studio新项目时提示:Plugin is too old, please update to a more recent version
    mvvm和mvc区别?
    什么是mvvm?
    js去除html标签
    数组的翻转(非reverse)
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2714801.html
Copyright © 2020-2023  润新知