zoukankan      html  css  js  c++  java
  • c# 学习之泛型笔记(一)

    用Hashtable类对比学习 泛型类 Dictionary<>, 并学习Dictionary的一些属性与方法。加深对泛型概念的理解。

      1 static void Main( string [ ] args )
      2 {
      3 
      4 //Hashtable stu_info = new Hashtable ();
      5 //stu_info.Add ( "A001", "张三" ); //方法添加 add(obj,obj)
      6 //stu_info.Add ( "A002", "李四" );
      7 //stu_info.Add ( "A003", "小明" );
      8 //stu_info["A004"]= "小红" ; // K-V 添加
      9 
     10 //foreach (DictionaryEntry xuehao in stu_info) //遍历用 “DictionaryEntry ”
     11 //Console.WriteLine ($"学号为{xuehao.Key},学生名字为:{xuehao.Value }" );
     12 
     13 Dictionary<string, string> Dstu_info = new Dictionary<string, string> (); //声明泛型 与Hsahtable一样,只是多了<参数类型1,参数类型2>.
     14 Dstu_info .Add ( "B005", "蜡笔小新" );
     15 Dstu_info.Add ( "B006", "樱木花道" );
     16 Dstu_info.Add ( "B007", "阿童木" );
     17 Dstu_info.Add ( "B008", "德马西亚" );
     18 Console.WriteLine ();
     19 
     20 // foreach 遍历输出key-value对,使用key,value属性。这两个属性不是Dictionary的,而是结构 KeyValuePair的属性。
     21 // Dictionary的属性是 Keys 与Values (有S,代表获得的是键和值的 集合)
     22 foreach (KeyValuePair <string,string> xuehao2 in Dstu_info) //使用 KeyValuePair 结构体。
     23 {
     24 Console.WriteLine ( $"学号为:{ xuehao2.Key},学生名字为:{ xuehao2.Value}" );
     25 }
     26 
     27 // 输出元素数,使用 count属性 
     28 Console.WriteLine ();
     29 Console.WriteLine ( $" 字典中键值对数量为{Dstu_info.Count}对" );
     30 
     31 //只输出值 使用 values属性 :获得字典中值的集合。
     32 Dictionary<string, string>.ValueCollection val = Dstu_info.Values;
     33 foreach(string str1 in val)
     34 {
     35 Console.WriteLine ( $"字典中所有值为-- {str1}" ); 
     36 }
     37 Console.WriteLine ();
     38 
     39 //只输出键,使用keys属性 :获得字典中键的集合。
     40 Dictionary<string, string>.KeyCollection key1 = Dstu_info.Keys;
     41 foreach (string str2 in key1)
     42 {
     43 Console.WriteLine ( $"字典中所有键为-- {str2}" );
     44 }
     45 
     46 // 通过key查找值,使用Item[Tkey]属性
     47 //containsKey() 方法确定是否有这个值。Bool 
     48 if (Dstu_info.ContainsKey ( "B005" )) 
     49 {
     50 Console.WriteLine ( "学号为B005的学生姓名为:{0}", Dstu_info [ "B005" ] );
     51 }
     52 else { Console.WriteLine ( "没有这个学号的学生" ); }
     53 Console.WriteLine ();
     54 
     55 //containValue(tValue)方法查找是不是有某确定值。Bool
     56 if (Dstu_info.ContainsValue ( "蜡笔小新" )) 
     57 {
     58 Console.WriteLine ( "有这名学生:蜡笔小新" );
     59 }
     60 else { Console.WriteLine ( "没有这个学生,请检查" ); }
     61 Console.WriteLine ();
     62 
     63 //tryGetvaleu(Key,out value) 获取与指定的键 关联的值。有返回值,无返回false. bool
     64 //相当于containsKey 与 Item[Tkey]的结合
     65 
     66 string strGet = " "; //定义空字符串用来存储数据
     67 if (Dstu_info.TryGetValue("B006",out strGet))
     68 {
     69 Console.WriteLine ( $"学号为A008的学生为:{strGet}" );
     70 }
     71 else
     72 {
     73 Console.WriteLine ( "没有这个号" );
     74 }
     75 
     76 //删除某一个键与值 Remove()方法
     77 //使用 KeyValuePair<Tkey,Tvalue> ,可以获得配对的 键&值
     78 //使用KeyValuePair 的属性.Key / .Value,可以分别获得键&值;
     79 Dstu_info.Remove ( "B007" );
     80 foreach (KeyValuePair<string,string> xuehao3 in Dstu_info )
     81 {
     82 Console.WriteLine ( $"{xuehao3}" ); // 输出配对的键&值 ,形式为【键,值】
     83 }
     84 //第2种,用属性分别输出 键和值 
     85 Console.WriteLine ();
     86 foreach (KeyValuePair<string, string> xuehao3 in Dstu_info)
     87 {
     88 Console.WriteLine ( $"学号:{xuehao3.Key} 姓名:{xuehao3.Value}" ); //key:键 value:值
     89 }
     90 Console.Read ();
     91 }
     92 }
     93 } 
     94 //以上, 用来表示 键与值的集合--泛型类 Dictionary<TKey,TValue> 类,知识点总结如下:
     95 /* 
     96 * 1) 属 性:Count Keys Values Item[Tkey]
     97 * 
     98 * 2) 构造函数: Dictionary<TKey, TValue>()
     99 * 
    100 * 3) 方 法:Add(TKey, TValue) , ContainsKey(TKey) ,ContainsValue(TValue) , 
    101                   TryGetValue(TKey, TValue), Remove(Tkey) , Clear()
    102 
    103 * 4) 结 构 体:KeyValuePair<TKey, TValue>, 以及它的属性 Key, Value
    104 */

    //输出结果

  • 相关阅读:
    socketpair和pipe的区别
    C++异常与析构函数及构造函数
    System v shm的key
    不可靠信号SIGCHLD丢失的问题
    非阻塞IO函数
    Android 编译时出现r cannot be resolved to a variable
    找工作笔试面试那些事儿(5)---构造函数、析构函数和赋值函数
    unable to load default svn client 和 Eclipse SVN 插件与TortoiseSVN对应关系
    演示百度地图操作功能
    求第i个小的元素 时间复杂度O(n)
  • 原文地址:https://www.cnblogs.com/FengTime/p/12456208.html
Copyright © 2011-2022 走看看