C#泛型(一)
简介:
先看看泛型的概念--“通过参数化类型来实现在同一份代码上操作多种数据类型。利用“参数化类型”将类型抽象化,从而实现灵活的复用”。
很多初学者在刚开始接触泛型的时候会比较难理解 “泛型” 在这里先把 “泛型”当作一个形容词 这样比较方便理解 因为很多东西都可以是泛型的 比如--
“泛型的类” ,“泛型的方法”,“泛型的接口”,“泛型的委托” 等...很多时候我们使用泛型可以极大减少代码重复使程序更加清爽,也可以避免不必要的‘装箱’ ‘拆箱’过程。
<泛型的引入|为什么要有泛型?>
在程序设计的过程中我们常常会遇到这样的情况:为了实现某一个功能我们一开始把方法写好,但后来我们发现同样的功能需要我们再写一次但是这次方法的参数类型和上次不一样了,这个时候按照敏捷软件开发的思想,不要过早的进行抽象和应对变化,当变化第一次出现时,使用最快的方法解决它,但变化第二次出现的时,在进行更好的架构设计,这样的目的是为了避免过度设计,因为有可能第二次变化永远也不会出现。考虑到功能一样,所这里我们通常会直接复制原方法的代码,然后修改一下参数类型即可快速解决;这样做确实没错,但是有的时候不仅出现了第二次变化 还出现了第三次...或者是更多次变化,继续使用CV大法修改方法的签名将会导致大量重复代码的出现,于是我们就会想,要是存在一个可以传递任何数据类型的方法那多好,即把这个方法的实现当成模板 把方法的签名抽象出来,于是我们引入了泛型。
下面我们来看一下具体的例子:
1.普通的写法:
-输入多个 int类型,进行冒泡排序让它们依次重小到大输出,代码如下:
public class SortHelper
{
public void BubbleSort(int[] arr)
{
int length = arr.Length;
for (int i = 0; i < length-1; i++)
{
for (int j = 0; j < length-1-i; j++)
{
if (arr[j]>arr[j+1])
{
int temp=arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
测试:
//控制台程序输出
static void Main(string[] args)
{
SortHelper sorter = new SortHelper();
int[] a = { 4,5,1,3,2,8,5,0,2}; sorter.BubbleSort(a);
}
输出为:0,1,2,2,3,4,5,5,8
---------------输入多个 Byte类型,进行冒泡排序让它们依次重小到大输出,代码如下:
这个时候我只要复制一下原来的方法改一下签名就可以了
public class SortHelper
{
public void BubbleSort(byte[] arr)
{
int length = arr.Length;
for (int i = 0; i < length-1; i++)
{
for (int j = 0; j < length-1-i; j++)
{
if (arr[j]>arr[j+1])
{
byte temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
这样做虽然可以,但是往后若要 处理N次各种其他 数据类时就 就要大量重复复制 严重影响代码的简洁度,而且当功能要扩展时 ,每个方法都要修改,维护起来非常不方便。
1.2使用泛型(泛型类):
我们自然而然的会这样想了如果可以把方法中的 参数类型 用一个 ”占位符“ 表示 每次 传入 什么类型 他就变成什么类型,这样就可以将这个方法当成一个模板用了(有点像Web编程中在Html中使用占位符)。
这里我们用 “T” 来便是这个特殊的参数类型,于是代码就变成了这样:
public class SortHelper
{
public void BubbleSort(T[] arr)
{
int length = arr.Length;
for (int i = 0; i < length-1; i++)
{
for (int j = 0; j < length-1-i; j++)
{
if (arr[j]>arr[j+1])
{
T temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
这里 T 代表 ”类型的类型“ 和 int ,string ...等数据类型相似,T 就是类型本身。让人兴奋的是真的有像 “T” 这样的特别存在,在.NET中叫做类型参数. 下面我们看看规范的代码--
这里我们把BubbleSort定义成泛型类 定义泛型类的一种方法是在类后面加上“<T>”
//定义泛型类SortHelper 这里“where T:IComparable” 是给类型参数T一个限制 -- 参数类型必须实现IComparable接口,否则无法通过编译
public class SortHelper<T> where T:IComparable
{
public void BubbleSort(T[] arr)
{
int length = arr.Length;
for (int i = 0; i < length-1; i++)
{
for (int j = 0; j < length-1-i; j++)
{
if (arr[j].CompareTo(arr[j+1])>0)
{
T temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
复制代码
测试:
static void Main(string[] args)
{
SortHelper<byte> sorter = new SortHelper<byte>();
byte[] a = { 4,5,1,3,2,8,5,0,2}; sorter.BubbleSort(a); SortHelper<int> sorter1 = new SortHelper<int>();
int[] b = { 4, 5, 1, 3, 2, 8, 5, 0, 2 }; sorter1.BubbleSort(b);
//输出省略
}
输出为:
0,1,2,2,3,4,5,5,8
0,1,2,2,3,4,5,5,8
---------------输入多个 自定义类型的实例,进行冒泡排序让它们依次重小到大输出,代码如下:
下面我们来模拟一下宠物店卖的猫 按价格排序
猫类:
public class cat:IComparable
{
public string name;
public int price; public int CompareTo(object obj)
{
cat catT = (cat)obj;
return this.price.CompareTo(catT.price);
} public cat(string name, int price)
{
this.price = price;
this.name = name;
}
}
测试:
static void Main(string[] args)
{
SortHelper<cat> sorter2 = new SortHelper<cat>();
cat cat1=new cat("猫1",1000);
cat cat2=new cat("猫2",1400);
cat cat3=new cat("猫3",400);
cat[] c = { cat1, cat2, cat3 }; sorter2.BubbleSort(c);
//输出
for (int i = 0; i < c.Length; i++)
{
Console.WriteLine("Name:"+c[i].name+" Price:"+c[i].price);
}
}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATgAAACfCAIAAADic1LfAAANPklEQVR4nO2av28iPRrH58+hd8U2NFGkjVLRRRuJDp1WVNw2VJFWQlsE6XRprjhF2WY7mqN4l25paE6ctOIarso1VHRU081b8MtjP35sD8l4zHw/ehQZ84x/TPzBMwNJBgCoPEnoAQAA7EBUACIAogIQARAVgAiAqABEAEQFIAIgKgARAFEBiACICkAEQFQAIgCiAhABEBWACICoAEQARAUgAnxFTefDdm80XW1PVZtJTwgxWuYTlyPBIuWvXm6b13c/VnzPu24s9CYbzwkBEAOeor6OO0I0e1NZB0bUgze5FDV/+2sghBj82uYO1fSTD1tPek0hOuO1qUMALgsvUTfTXlOI3mSdcRvcThYnUalGRkv50FP5eNh6+tBWjmmPlilEBReMh6jrSa8pmr3JOltPBr2H4XiZZll29qXv6/hOiNuXVZatXm6tovYfHtp3g2G/3eqPV9t0s3jqXPen63zmdHDdFK3+FNKCS8FD1J1Hhz1sOD9ocN6lbzofHi58D4dYdtQsXS9+9I+barPzNH/d5kXttwREBZeE5z1quhy1Ras/fk2N1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LuDxCiirjYl3i7La7daYcJt/qIZ8gz4Vv0DopUB8CiCrDK6fXu+9CfALTO/9BQLbski+PX/9LjtM6KVAfAot6hF+F+prmF7SjqAXgPziUSt5zq/N6jd4gqAmliqqvWseF6CVeAVG9hkHKxltknbLXaF0SwIVRqqi7LgsssuNqthplTWMMJPtlKvWm+HymzHeqz448EFwwgUV13F4Yr8gys9ZJ3H3W35pRHw2mfhnJ+XFC0ZoTXlRlQLrGehrfgrVZl3yXxvWxWcU2/SXzre2D+hBeVHJ1KrjsUXylVVQTTONHkZgD9dHqb1lVNLUG6kN4UZUBuYhH7jZ8I45L3CXN91PD8VhTvvUThPlM4dt8p3zwHgQTldmO9FG6V5re4pX2+uxQxu/Yi0tHSj7TrzWZAaLGSBhRj/91R6NMabx+1mbJdsiyUsm4qjSuLHHrJ4LLRxWcqSFhRD12r5ddthHrqj0eZUI51io8P06XLphMJt9UCVfrRgBRZRgDyUpyw3mTdcz7EwrTGIIPDJRMYFEBAC5AVAAiAKICEAEQFYAIgKgARABEBSACICoAEQBRAYgAiApABEBUACIAogIQARAVgAh4e1EBAKGAqABEAEQFIAIgKgARAFEBiACICkAEQFQAIgCiAhABEBWACICoAETAn68HT1gxOIU+AAAAAElFTkSuQmCC" alt="" />
*泛型与集合类型(ArrayList)
概要:通过泛型可以大大提高集合类型的的性能恶化安全性。
下面我们来看一个例子
2.1 非泛型的集合类
先是 往集合里 存放 3 个数据
ArrayList list = new ArrayList();
int listSize = 3; for (int i = 0; i < listSize; i++)
{
list.Add(i);
} for (int i = 0; i < listSize; i++)
{
int value = (int)list[i];
Console.WriteLine(value);
}
测试:
输出
0
1
2
有经验的读者在这里可能会注意到了,这样子写虽然能运行通过,但是这里当 list每次调用Add方法时就做了一次 ” 装箱 “ 操作,接着每次取数据时对list的元素进行一次强制转换 (int)list[i] 同时也做了一次 “ 拆箱 ”操作,这两个操作对.NET来说是比较耗时的,当操作的次数越多效果就越明显;
2.2下面我们将 listSize 设置成 1000000 然后用 开始和结束DateTime.Now 来获取消耗的时间:
ArrayList list = new ArrayList();
int listSize = 1000000;
long StarTime = DateTime.Now.Ticks;
for (int i = 0; i < listSize; i++)
{
list.Add(i);
} for (int i = 0; i < listSize; i++)
{
int value = (int)list[i];
}
long EndTime = DateTime.Now.Ticks; Console.WriteLine("使用ArrayList,耗时:{0} Ticks", EndTime - StarTime);
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYEAAACyCAIAAAArouTEAAAO6klEQVR4nO3dMWszyR3H8X0J9w6SUt3Bs5XA4MZ+eIrHgah5EDinxpgUIoV5GhVBbtyYDUfcBEwuqtwpKRYeEAQiN25iXaM0SiDnNKrEJeBKRYhSyFqPdmdmZ0cajbz7/TAYaTWaHa32/3t2V9JdsAAAfwLfEwBQaWQQAJ/IIAA+kUEAfCKDAPhEBgHwiQwC4BMZBMAnMgiAT2QQAJ/IIAA+kUEAfCKDAPhEBgHwiQwC4BMZBMAnMgiAT2QQAJ/IIAA+kUEAfCKDAPhEBgHwiQwC4BMZBMAnNxk0G3ZbF/2nubLDOArDMBovFs+TuNu6iKeZEeJ2GIbRYHRzrh0IwNvm6DhoGrfrYb3dv+uGa6Lx8vEkg+bjqBGGYSMar+fMbNAOwzAaTQfdRqM7nJJCQDk5Oxebj29anf5dNwzb8WyxeDmyicbjaPV3FUiTXrPZHUxHUai2yi4AJeMkg2bDbqszmC6WxzvtOI7CMIpVGSQ3jsIkvmTm416zXm9Go+ftzx/Azrg5DpoOLg7Dw4vlwY0mg5ZXfaRhM76pazPoeRSRQcDb5+xcbBp32r3xIP84aHXAk+TR8uQrjtthWL/hHAwoN8efzRuci4knXS+fho3XbwIoL2cZNO61Onej+Oolgz72hsUyaLm8M+RUCyg1Vxk06X0Mw2b/aXU7CRPjDHrqN8PwPPvNIQBl4iiDnvrNMPzYmyxevir0sXXeOu9N5uoMGj/0hzMxg+YPV2EYRqPn4VX7ajBJf0Ho5Zp0b8w3h4C3zE0GJRH0PIoaYb0dT5/HUSMMG1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
*泛型与集合类型(ArrayList)
概要:通过泛型可以大大提高集合类型的的性能恶化安全性。
下面我们来看一个例子
2.1 非泛型的集合类
先是 往集合里 存放 3 个数据
ArrayList list = new ArrayList();
int listSize = 3; for (int i = 0; i < listSize; i++)
{
list.Add(i);
} for (int i = 0; i < listSize; i++)
{
int value = (int)list[i];
Console.WriteLine(value);
}
测试:
输出
0
1
2
有经验的读者在这里可能会注意到了,这样子写虽然能运行通过,但是这里当 list每次调用Add方法时就做了一次 ” 装箱 “ 操作,接着每次取数据时对list的元素进行一次强制转换 (int)list[i] 同时也做了一次 “ 拆箱 ”操作,这两个操作对.NET来说是比较耗时的,当操作的次数越多效果就越明显;
2.2下面我们将 listSize 设置成 1000000 然后用 开始和结束DateTime.Now 来获取消耗的时间:
ArrayList list = new ArrayList();
int listSize = 1000000;
long StarTime = DateTime.Now.Ticks;
for (int i = 0; i < listSize; i++)
{
list.Add(i);
} for (int i = 0; i < listSize; i++)
{
int value = (int)list[i];
}
long EndTime = DateTime.Now.Ticks; Console.WriteLine("使用ArrayList,耗时:{0} Ticks", EndTime - StarTime);
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAU4AAAChCAIAAAAnVMHdAAANI0lEQVR4nO2dvWsryxmH5084/8FNqe7AmUpgUGMfXNiBqDEC5aoxIoVIYU6jIsiNGrPhEjUBkxNV7pQUCxcEgciNmxydZm+jBHKVRpW4CahSEaIUK61n5+OdWUmrlTW/hxezmh3NvPvxzOyuZMRWAAAPYEUnAAA4BFAdAC+A6gB4AVQHwAugOgBeANUB8AKoDoAX2FSfjzqNu8F0aawQBZzzIFqtFpOw07gLZ0oLYYtzHgzHvSbZEAAgR6yz+ixslXm5NXjq8BRBFK9PVF9GQZVzXg2itM7zYYtzHoxnw0612hnNIDsABeBwAb+Meo324KnDeSucr1breTqIomDzd+P9pF+rdYazccDNbIYIAMAhsag+H3Ua7eFsFc/erTAMOA9Ck+p6ooAno4SOZdSvlcu1YLzYbhMAAHZss/pseFfhl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bCUfUtoIceqZAeKayjhlqiNnjC/AGeH4r9q66e946nciZ1t1A9UxpaXWkPrZucKVuXCifAzxj7Saf656ITOz3ymtW3OE0TH6xOWqsRDmv7JQrVpuj6xDLdqbp12jeeHr8xTOx/Zexd0bmdEodQ3XGKI8zULhO2aHEfEdRVz7rBxdQvMUzQefomecw7xuYG23/E87n9cSDVpV7VgUCtRrdgbdalvkvjam7WocH0V1vf2r4PfGt+OPdvxn5edHqnwbHM6upbTKuIQqvqJojGExWJN6rZqqusMpta84R/bJ7GVRj7pyL8r4tO7wQoflY3FWpnPLoRR0lcqmUddxzfa6pvHYOIUYluM6f6e6eSfur+S+Vx3Wfcuu9GvqoTU6KainuhaRU9KGQafaT8HXtx6UiqT/RrrUzw5lRXecdYh7H/CLZ/he07kKPqyXnj6KSpGi2wtVltO9plqZCwXWpcksQ6prgMdkdlXYF8w9hnxv4n3LrjQd125Durm5ZdpjLreZ+8y4T0XuuQQefp0gVRk6hvKoTtCe8ZGwm2f1t0Pm+RvFQXIRzWFmonvb2YQBtYFKYcCk/s2BCf2P226GTeHIdQHYA9kjyx+zNu3bMA1cHbI3li95Wxb4pO5q0A1cFbJX5i9xNjlaIzeRNAdfC2iZ/Y4UGdFagOgBdAdQC8AKoD4AVQHQAvgOoAeAFUB8ALoDoAXgDVAfACqA6AF0B1ALwAqgPgBVAdAC+A6gB4AVQHwAugOgBeANUB8AKoDoAXQHUAvACqA+AFe1AdAHAaQHUAvACqA+AFUB0AL4DqAHgBVAfAC6A6AF7wfxHEXVOnYAg3AAAAAElFTkSuQmCC" alt="" />
2.3 使用泛型集合类型(泛型数组)
List<int> list = new List<int>();
int listSize = 1000000;
long StarTime = DateTime.Now.Ticks;
for (int i = 0; i < listSize; i++)
{
list.Add(i);
} for (int i = 0; i < listSize; i++)
{
int value =list[i];
}
long EndTime = DateTime.Now.Ticks; Console.WriteLine("使用List<int>,耗时:{0} Ticks", EndTime - StarTime);
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhE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YyIiIhoBAxhRERERCNgCCMiIiIaAUMYERER0QgYwoiIiIhGwBBGRERENIL/D7shKfuoviDSAAAAAElFTkSuQmCC" alt="" />
总结:
看到这里相信大家明白为什么要引入泛型了吧,通过使用泛型-
1.可以避免同种功能代码的大幅度重复出现使我们的代码更加简洁/可读性更高
2.方便扩展维护,灵活度高
3.避免隐式的装箱拆箱,提高程序运行速度
C#泛型(一)的更多相关文章
- 一起学 Java(三) 集合框架、数据结构、泛型
一.Java 集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个 ...
- .NET面试题系列[8] - 泛型
“可变性是以一种类型安全的方式,将一个对象作为另一个对象来使用.“ - Jon Skeet .NET面试题系列目录 .NET面试题系列[1] - .NET框架基础知识(1) .NET面试题系列[2] ...
- C#4.0泛型的协变,逆变深入剖析
C#4.0中有一个新特性:协变与逆变.可能很多人在开发过程中不常用到,但是深入的了解他们,肯定是有好处的. 协变和逆变体现在泛型的接口和委托上面,也就是对泛型参数的声明,可以声明为协变,或者逆变.什么 ...
- 编写高质量代码:改善Java程序的151个建议(第7章:泛型和反射___建议106~109)
建议106:动态代理可以使代理模式更加灵活 Java的反射框架提供了动态代理(Dynamic Proxy)机制,允许在运行期对目标类生成代理,避免重复开发.我们知道一个静态代理是通过主题角色(Prox ...
- 6.在MVC中使用泛型仓储模式和依赖注入实现增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...
- C#泛型详解(转)
初步理解泛型: http://www.cnblogs.com/wilber2013/p/4291435.html 泛型中的类型约束和类型推断 http://www.cnblogs.com/wilber ...
- C# 泛型
C# 泛型 1.定义泛型类 在类定义中包含尖括号语法,即可创建泛型类: class MyGenericClass<T> { //Add code } 其中T可以遵循C#命名规则的任意字符. ...
- java8中lambda表达式的应用,以及一些泛型相关
语法部分就不写了,我们直接抛出一个实际问题,看看java8的这些新特性究竟能给我们带来哪些便利 顺带用到一些泛型编程,一切都是为了简化代码 场景: 一个数据类,用于记录职工信息 public clas ...
- java 泛型
1.Student stu =tool.getObj();右边得到的是Object类型,需要向下转型,强转换. 2. 3. 4.泛型方法不能被静态修饰这样写 5.如果想定义定义静态泛型方法,只能这样写 ...
- Java泛型的历史
为什么Java泛型会有当前的缺陷? 之前的章节里已经说明了Java泛型擦除会导致的问题,C++和C#的泛型都是在运行时存在的,难道Java天然不支持“真正的泛型”吗? 事实上,在Java1.5在200 ...
随机推荐
- ruby导出exl方式
class Demo print "hello world" require "spreadsheet" #设置表格的编码为utf-8 Spreadsheet. ...
- Solr之.net操作
http://www.cnblogs.com/zhangweizhong/category/771055.html 插入: SolrNet.Startup.Init<Movie>(&quo ...
- change丶未来科技公众号成立了!!!!!!!!!
其实笔者好早之前就开始弄个公众号了,但是出于各种各样的原因,一直被耽搁,现在可以了.总算弄好了,弄个公众号不难,其他原因而已 欢迎大家在Change丶未来科技交流成长. 互联网博文全新模式,分享你我的 ...
- mysql 案例 ~ 瘦身mysql系列(1)
一 简介:这一系列我们要进行如何瘦身mysql 二 目的:通过提高CPU利用率和节约成本,降低数据库容量及I/O负载,从而使数据吞吐率得到显著提高 三 方法: 利用innodb的COMPRESSED ...
- python - 上下文管理协议(with + __enter__ + __exit__)
上下文管理协议: with + __enter__ + __exit__ #上下问管理协议: #with + __enter__ + __exit__ class Test(): def __init ...
- ResNet152网络复现(Caffe)
一.准备数据集 1) 下载数据集 Imagnet网站上下载了三类图片,分别是big cat.dog.fish,其中训练集的图片数一共是4149,测试集的图片数是1003,训练集和测试集的图片数比例4 ...
- Django开发笔记一
Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.运行 python manage.py runser ...
- 蒙特卡罗定位(Particle Filter Localization)笔记
善始善终,这篇文章是Coursera课程Robotics: Estimation and Learning最后一周的课程总结.里面的小哥讲得不是很清晰,留下的作业很花功夫(第二周课程也是酱紫). 这周 ...
- 【CTF WEB】ISCC 2016 web 2题记录
偶然看到的比赛,我等渣渣跟风做两题,剩下的题目工作太忙没有时间继续做. 第1题 sql注入: 题目知识 考察sql注入知识,题目地址:http://101.200.145.44/web1//ind ...
- Pytorch之可视化
先解决下keras可视化安装graphviz的问题: 注意安装顺序: sudo pip3 install graphviz # python包 sudo apt-get install graphvi ...