<1>.泛型方法

以前文章说到用一个泛型类 SortHelper 来做一个冒泡排序的处理,下面回顾一下之前的代码:

 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;
}
}
}
}
}

这里我们为了调用 BubbleSort,而将参数T传递给类 SortHelper,假如 SortHelper 类中,除了 BubbleSort 方法之外,还有其他的没有使用到参数T的方法 比如:

 public class SortHelper<T> where T:IComparable
{
public void BubbleSort(T[] arr)
{
//省略
} public void OtherMethod1()
{
//省略
}
//此处省略N个OtherMethod
}

这里有 N 个 OtherMethod方法。

测试-调用 OtherMethod1:

static void Main(string[] args)
{
SortHelper<int> UserOtherMethod = new SortHelper<int>();
UserOtherMethod.OtherMethod1();
}

在实例化 UserOtherMethod 对象时 必须给 SortHelper 传一个类型参数 这里是 int  。显然这中情况下,我们仅仅是为了 BubbleSort 方法正常使用而定义泛型类,从而强迫其他使用 SortHelper 类其它方法

实例传递一个参数,很明显这是没必要的,于是这个时候 ’泛型方法‘ 就出现了 ,修改代码如下:

 public class SortHelper2
{
public void BubbleSort<T>(T[] arr) where T:IComparable
{
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;
}
}
}
}
}

总结:

泛型方法和普通方法没多大区别,仅仅在方法名后面加了“<T>”, 接着是括号里面的参数,最后是限定语句。

上述的这种情况,我们通过使用泛型方法,就可以避免每次使用泛型类传参了。

<2>.泛型接口

自己写了一个 “cat” 类,并调用了一个接口“IComparable” 代码如下:

 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;
}
}

这里粉红色标记的部分我们实现了 “IComparable” 接口的 “CompareTo” 方法 这里没有进行装箱和拆箱。

因为cat是引用类型,而且“ return this.price.CompareTo(catT.price) ”这里调用的是 price (int型) 的 ComparaTo 方法,右键对int转到定义查看代码。

这里可以清楚的看到int类型实现了 IComparable 与 IComparable<int> 这两个接口,也就是说它分别实现了这两个接口的 ComparaTo() 方法,那这里又是调用那个接口的 ComparaTo() 方法呢?

实际上这里传过来的参数是int类型,那么优先调用的是 ComparaTo(int value) 方法,这样就避免了不必要的装箱拆箱过程了。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0IAAAH0CAIAAACfMApiAAAgAElEQVR4nO3d3bHjNoIGUGXgeZln+91V443A3o3AzsBdG4C9Edgh2BNBu2rfxxOBHUJnsJ1BhyDtw22refFHgKRIAjqnulwUBAIg9IPP5JV0uQEA0KHL0QMAAGAJMQ4AoEtiHABAl8Q4AIAuiXEAAF0S4wAAuiTGAQB0SYwDAOiSGAcA0CUxDgCgS2IcAECXxDgAgC6JcQAAXRLjAAC6JMYBAHRJjAMA6JIYBwDQJTEOAKBLYhwAQJfEOACALolxAABdEuMAALq0JMZd/rL5aBiep03vPIIFD50cM8+dVZi7hTEuKPnyn7/f/y0bR82OhS6W9Vtobc2xlLt7RMuV/e7ZadL9mXPskKa9x8NoGlXNUVwmFoz2QWoGkxv2ygNZ/EKIH7hpC/WtBXtt9Saww+O7Txc7P10Xd5QcZ/3gL68tG0Pc4JqarYM51VsKR9kgxi17J11s2y4KrT2uo0fPWNzmnoGp0Nf0mXPskO4l64dRbmF6yIe8567vdNsYt/LJ/4jXzlZvAlGeWD6kyi4e5N5LFykhl4fW7L7tYFprinE02TjG7WCAGPe4XnJtniHGHfi0yU3IJmOoj3GHeESMW9zstqH5VDEuFXaXD6mpo4d2cfgTeJYYx5Pb7KJqXC151SO+kpW7eFEon602vWoTDyBZUhN9tr1wXB5kciPYjsdfaDM3pNxjUbgr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MQ4AoEtiHABAl8aMccnv4AAAGMmwWUeMAwDGNmzWEeMAgLENm3XEOABgbMNmHTEOABjbyFlHkgMABjZs0JHhAICxDZt1xDgAYGzDZh0xDgAY27BZR4wDAMY2ZtbxKw4AwPBkHQCALolxAABdEuMAALokxgEAdEmMAwDokhgHANAlMW4/l78cPRAAYAQixW2fWDVNb5IcALCePCHGAQBdGjNPXC4fw9l9Y1p+L7nfjAsL24WN4pCyMU6qAwAWGDZAxNEqyHPJ7dmacZvTnFccj7NxAMCWhs0TyXAWn3vL1Zzdrrn3dU0xDgDY0rB5ohy56muKcQDAOQ2bJxaEs+SGGAcAnNOYeSJ58XRanqyfLAk2kp+QKLT8us3098ZJdQDAAgLE8cQ4AGABAQIAoEtiHABAl8Q4AIAuiXEAAF0S4wAAunR8jEt+BwcAAGWnyE9iHABAq1PkJzEOAKDVKfKTGAcA0OoU+UmMAwBodZb8JMkBADQ5RXiS4QAAWp0iP4lxAACtTpGfxDgAgFanyE9iHABAq+Pzk19xAABYQH4CAOiSGAcA0CUxDgCgS2IcAECXxDgAgC6JcQAAXRLjAAC6JMYBAHRJjAMA6JIYBwDQJTEOAKBLYhwAQJfEOACALolxAABdEuMAALokxgEAdEmMAwDokhgHANCl/weEaMGYWtedhgAAAABJRU5ErkJggg==" alt="" />

上图中是.NET 中 int 类型实现 IComparable 接口的 ComparaTo(object value) 方法,不难看出“红箭头”标记部分是执行装箱拆箱操作的过程。

下图是 int类型实现泛型版本的 IComparable<int> 的 ComparaTo(int value) 方法,如果对2种方法进行大量重复操作,不难发现使用泛型接口实现的方法性能比较好。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA1AAAAD6CAIAAAAgFjFzAAATAklEQVR4nO3dP4/cyJkH4P4Gq2RjKzdgwV9Aq80PkNKLduHYkDa9ZKHksgNm7xNoAOVawE4cSdHFA1x0kRUdDPgABY4csS+YVg+H9YdFspvsqn4eEAM2u8h6yZ4Z/sBid+/2UIO//fnm7c2f/7Z1GRS4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zMxnZgoKi7STAgGAcnLDgzCEDZJfdH60ZbjNfiIsqMprBAAsIkw8iMa48HpequXofMmzsaq8RgDAIsLEg3w4K28p8AEAF0WYeDAjxkVnBD4A4KIIEwfRodv+8mj76JLBTPQ9H5ktP97mLvrJLFIgAFBObqiSwAcAlJMbAAAat9v8O4NNJpPJZDKZrnYS+Ewmk8lkMpkan1YKfOt0AwDAVgQ+AIDGVRn4op9UAgBAVK2xSeADAChUa2wS+AAACtUamwQ+AIBCtcYmgQ8AoFDFsUnmAwAoUWtmkvYAAArVGpsEPgCAQrXGJoEPAKBQrbFJ4AMAKFRlbPJNGwAA5cQmAIDGCXwAAI0T+AAAGifwAQA0TuADAGicwAcA0DiB72R2X21dCADAI+2nk3UCWD/nyXwAwEVpP5oIfADAlasymux2hxh3nOkvPy45PgwXZuYzM9mSkoFP/gMAtlVrFglD2CD5RedHW4bb7CfCbD2u8AEAF6rWaBKNceH1vFTL0fmSZx+3FPgAgAtVazTJh7PylgIfANC8WqPJjBgXnRH4AIDmVRlNokO3/eXR9tElg5noez4yW368zfjn8Ml/AMC2ZJGzE/gAgG3JIgAAjRP4AAAaJ/ABADRO4AMAaJzABwDQuLMHvugnlQAAsJo1opjABwCwIYEPAKBxAh8AQOMEPgCAxq0UxWQ+AICtuMIHANA4gQ8AoHECHwBA4wQ+AIDG+aYNAIDGiWIAAI0T+AAAGifwAQA0TuADAGicwAcA0DiBDwCgcQIfAEDjBD4AgMYJfAAAjRP4AAAaJ/ABADRO4AMAaJzABwDQOIEPAKBxAh8AQOMEPgCAxgl8AACNE/gAABon8AEANE7gAwBonMAHANA4gQ8AoHH/DxoUfBq9ulTfAAAAAElFTkSuQmCC" alt="" />

总结:

程序设计中应该尽量避免装箱和拆箱操作,为了避免多次装箱拆箱造成的性能影响 ,通过上面的例子我们可以知道使用泛型接口可以有效的避免这个问题:使用IComparable<T> 而不使用 IComparable,这样可以避免值类型的装箱和取消装箱操作。

C#泛型(二)的更多相关文章

  1. [C# 基础知识系列]专题八: 深入理解泛型(二)

    引言: 本专题主要是承接上一个专题要继续介绍泛型的其他内容,这里就不多说了,就直接进入本专题的内容的. 一.类型推断 在我们写泛型代码的时候经常有大量的"<"和"& ...

  2. Java 泛型 二

    一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: 1 public class GenericTest { 2 3 public static void main(Stri ...

  3. java 面向对象(三十三):泛型二 泛型在集合中的使用

    1. 在集合中使用泛型之前的例子 @Test public void test1(){ ArrayList list = new ArrayList(); //需求:存放学生的成绩 list.add( ...

  4. Java泛型二:通配符的使用

    原文地址http://blog.csdn.net/lonelyroamer/article/details/7927212 通配符有三种: 1.无限定通配符   形式<?> 2.上边界限定 ...

  5. C#高级编程之泛型二(泛型类型约束、类型、反射与泛型)

    泛型类型约束 简言之:对泛型类型进行约束,细化,限定. MSDN的定义:泛型定义中的 where 子句指定对用作泛型类型.方法.委托或本地函数中类型参数的参数类型的约束,意思就是可以有泛型类.泛型方法 ...

  6. Java(7)泛型

    目录 一.泛型概述 1.什么是泛型 2.为什么用泛型 3.在集合中使用泛型 二.自定义泛型结构 1.泛型类.接口 2.泛型方法 三.举例泛型类和泛型方法的使用场景 1.泛型类举例: 2.泛型方法举例 ...

  7. 理解C#泛型运作原理

    前言  我们都知道泛型在C#的重要性,泛型是OOP语言中三大特征的多态的最重要的体现,几乎泛型撑起了整个.NET框架,在讲泛型之前,我们可以抛出一个问题,我们现在需要一个可扩容的数组类,且满足所有类型 ...

  8. javaweb——总结

    day01XML上    1.XML的作用    2.XML的基本语法    3.DTD约束    4.DTD的基本语法(看懂DTD就ok)    5.XML的解析方式:原理    6.JAXP的DO ...

  9. JAVA基础学习day15--集合二 TreeSet和泛型

    一.TreeSet 1.1.TreeSet Set:hashSet:数据结构是哈希表.线程是非同步的.        保证元素唯一性的原理:判断元素的HashCode值是否相同.         如果 ...

  10. Java学习笔记(二一)——Java 泛型

    [前面的话] 最近脸好干,掉皮,需要买点化妆品了. Java泛型好好学习一下. [定义] 一.泛型的定义主要有以下两种: 在程序编码中一些包含类型参数的类型,也就是说泛型的参数只可以代表类,不能代表个 ...

随机推荐

  1. JAVA String作业——动手动脑以及课后实验性问题

    一:如何解释这样的输出结果?从中你能总结出什么?1.源码 //mao2015.10.20 //==的两个分辨 //对原始数据而言 //对引用类型变量 public class StringPool { ...

  2. Gas Station [LeetCode]

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  3. 10大html5前端框架

    Bootstrap 首先说 Bootstrap,估计你也猜到会先说或者一定会有这个( 呵呵了 ),这是说明它的强大之处,拥有框架一壁江山的势气.自己刚入道的时候本着代码任何一个字母都得自己敲出来挡我者 ...

  4. HTTP && socket

    http://blog.csdn.net/zeng622peng/article/details/5546384 1.TCP连接 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终 ...

  5. struts2视频学习笔记 22-23(基于XML配置方式实现对action的所有方法及部分方法进行校验)

    课时22 基于XML配置方式实现对action的所有方法进行校验   使用基于XML配置方式实现输入校验时,Action也需要继承ActionSupport,并且提供校验文件,校验文件和action类 ...

  6. NABCD模型(猫咪记单词)

    项目需求分析与建议-NABCD模型(猫咪记单词)   N (Need 需求) 对于现在的学生,尤其是大学生来说,学习英语是一件非常重要的事.我们有四级六级托福雅思等各种各样的英语方面的考试.而学习英语 ...

  7. DOM系列---进阶篇【转】

    内容提纲: 1.DOM类型 2.DOM扩展 3.DOM操作内容 一.DOM类型 DOM基础篇中,我们了解了DOM的节点并且了解怎样查询和操作节点,而本身这些不同的节点,又有着不同的类型.   DOM类 ...

  8. Linux下配置用msmtp和mutt发邮件

    Linux下可以直接用mail命令发送邮件,但是发件人是user@servername,如果机器没有外网的dns,其他人就无法回复.此时,有一个可以使用网络免费邮箱服务的邮件发送程序就比较重要了.ms ...

  9. double int char 数据类型

    贴心的limits... 测试代码: #include <iostream> #include <stdio.h> #include <limits> #inclu ...

  10. API 进程、线程函数

    CancelWaitableTimer 这个函数用于取消一个可以等待下去的计时器操作 CallNamedPipe 这个函数由一个希望通过管道通信的一个客户进程调用 ConnectNamedPipe 指 ...