C#程序设计 第5章泛型
C# 程序设计 第5章 泛型
泛型 ★ 泛型引例 泛型方法 泛型类 泛型集合 2
2 泛型 泛型引例 泛型方法 ★ 泛型类 泛型集合
本章目标 ■ 理解泛型的基本概念及使用泛型的优点 ■掌握泛型方法的定义和使用 ■掌握泛型类的定义和使用 ■掌握常用的泛型集合的使用
3 本章目标 ▪ 理解泛型的基本概念及使用泛型的优点 ▪ 掌握泛型方法的定义和使用 ▪ 掌握泛型类的定义和使用 ▪ 掌握常用的泛型集合的使用
泛型 ★ 泛型方法 4
4 泛型 ★ 泛型方法
引例 间题 思考:编写交换两个int数的方法? 编写交换两个doub I e数的方法? static void Swap(ref int x,ref int y) static void Swap(ref double x,ref double y) { { int temp; double temp; temp =x; temp =x; x=y; x=y; y=temp; y=temp; 交换两个shor t数的方法? 带来的问题:代码膨胀 交换两个decimal数的方法?
5 引例 static void Swap(ref int x, ref int y) { int temp; temp = x; x = y; y = temp; } static void Swap(ref double x, ref double y) { double temp; temp = x; x = y; y = temp; } 思考:编写交换两个int数的方法? 编写交换两个double数的方法? 交换两个short数的方法? 交换两个decimal数的方法?. 带来的问题:代码膨胀