- 2
- 0
- 约1.54万字
- 约 46页
- 2018-06-06 发布于河北
- 举报
第2章(五) 数据类型与流程控制
【例3-8】泛型的定义与使用。 using System; using System.Collections.Generic; using System.Collections; using System.Text; namespace ConsoleApplication2{ class StackT:CollectionBase{ private T[] store; private int size; public Stack(){ store = new T[10]; size = 0; } public void Push(T x){ store[size++] = x; } public T Pop(){ return store[--size]; } } } namespace ConsoleApplication2{ class Program{ [STAThread] static void Main(string[] args){ Stackstring stack = new Stackstring(); stack.Push(ni hao); stack.Push(wo buhao); Console.WriteLine(stack.Pop()); Console.WriteLine(stack.Pop()); Console.Read(); } } } 可空泛型 可空类型是泛型结构NullableT的实例。 例如,NullableInt32读作“可以为null的Int32”,可以将其赋值为任一个32位整数值,也可以将其赋值为null值。 可空类型具有以下特性: (1)可空类型表示可被赋值为null值的值类型变量。但是要注意,由于引用类型已支持null值,因此不能用该类型创建基于引用类型的null类型。 (2)语法“T?”是泛型“NullableT”的简写,此处的T为值类型。这两种形式可以互换。如Nullableint也可以写为int?。 (3)程序员可以使用泛型的HasValue和Value只读属性测试是否为空和检索值,如果此变量包含值,则HasValue属性返回True;如果此变量的值为空,则返回False。 2.4.5 泛型集合 在System.Collections.Generic命名空间下,提供了常用的泛型集合类。 泛型集合类 非泛型集合类 泛型集合用法举例 ListT ArrayList Liststring dinosaurs = new Liststring( ); DictionaryTKey,Tvalue Hashtable Dictionarystring, string d = new Dictionarystring, string( );d.Add (txt, notepad.exe); QueueT Queue Queuestring q = new Queuestring( );q.Enqueue(one); StackT Stack Stackstring s = new Stackstring( );s.Push(one);s.Pop( ); SortedListTKey,TValue SortedList SortedListstring, string list = new SortedListstring, string( );list.Add (txt, notepad.exe); list.TryGetValue(tif, out value)) 哈希集合 HashSet HashSetT泛型类提供了高性能的数学集合运算,一个HashSetT对象的容量是指该对象可以容纳的元素个数。 常用方法: UnionWith方法:并集或Set加法 IntersectWith方法:交叉 ExceptWith方法:set减法 SymmetricExceptWith方法:余集 列表和排序列表 ListT泛型类表示可通过索引访问的强类型对象列表,该类提供了对列表进行搜索、排序和操作的方法。 常用方法如下: Add方法:将指定值的元素添加到列表中。 Insert方法:在列表的中间插入一个新元素。 Conta
您可能关注的文档
- 神奇的眼睛应用光学__人眼特性.ppt
- 科学技术与社会 许妙成.ppt
- 秘书公文办 理.ppt
- 程控九章1.ppt
- 空间8节点等参单元计算Drucker—Prager理想塑性材料.pdf
- 突发饮用水污染事 件与卫生监督(徐贻萍)9.10.ppt
- 磁性材料与EMC.pdf
- 第03章 信息技术基础.ppt
- 第 8 章 晶体的点阵结构与X射线衍射.pdf
- 第03章-Oracle安装、配置和建立.pdf
- 小区绿化施工协议书.docx
- 墙面施工协议书.docx
- 1 古诗二首(课件)--2025-2026学年统编版语文二年级下册.pptx
- (2026春新版)部编版八年级道德与法治下册《3.1《公民基本权利》PPT课件.pptx
- (2026春新版)部编版八年级道德与法治下册《4.3《依法履行义务》PPT课件.pptx
- (2026春新版)部编版八年级道德与法治下册《6.2《按劳分配为主体、多种分配方式并存》PPT课件.pptx
- (2026春新版)部编版八年级道德与法治下册《6.1《公有制为主体、多种所有制经济共同发展》PPT课件.pptx
- 初三教学管理交流发言稿.docx
- 小学生课外阅读总结.docx
- 餐饮门店夜经济运营的社会责任报告(夜间贡献)撰写流程试题库及答案.doc
最近下载
- ntvf调试资料解读.doc VIP
- 电影《色.戒》的叙事伦理.doc VIP
- 上海大学2023-2024学年第1学期《高等数学(上)》期末考试试卷(A卷)附参考答案.pdf
- 佳能EOS1500D基本使用说明书说明书.pdf VIP
- 上海大学2023-2024学年第1学期《高等数学(上)》期末考试试卷(B卷)附参考答案.pdf
- 渣打银行(香港)有限公司.PDF VIP
- 2024年吉林高职高专院校单招统一考试《语文》试卷及参考答案.pdf VIP
- JSG202515机器人系统集成应用技术样题-学生组.docx VIP
- 棋盘博弈采购法战略体系.ppt VIP
- 节后复产复工安全知识考试试卷及答案.docx
原创力文档

文档评论(0)