语法注意点

  • 可以使用扩展方法来扩展类或接口。
  • 不能重写扩展方法。
  • 扩展方法只能在非嵌套、非泛型静态类内部定义。
  • 扩展方法必须定义在静态类中。
  • 扩展方法的第一个参数的类型用于指定被扩展的类型,它限制该扩展方法只能作用于该类型。
  • 扩展方法的第一个参数必须带有 "this" 修饰符。
  • 调用方必须引入扩展方法的命名空间。
  • 调用扩展方法的语法,与调用 "被扩展的类型上的实例方法" 的语法相同。
  • 调用扩展方法时,无需传递第一个参数,因为该参数仅用于指定被扩展的类型。
  • 扩展方法是一种特殊的静态方法,其调用方式与调用被扩展类型上的实例方法语法相同。
  • 仅当使用 using 指令将命名空间显式导入到源代码中之后,扩展方法才位于范围中。

 

Humanizer(人性化)

Humanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities

 

string

// Return a string describing the value as a file size.
// For example, 1.23 MB.
public static string ToFileSize(this double value)
{
string[] suffixes = { "bytes", "KB", "MB", "GB",
"TB", "PB", "EB", "ZB", "YB"};
for (int i = 0; i < suffixes.Length; i++)
{
if (value <= (Math.Pow(1024, i + 1)))
{
return ThreeNonZeroDigits(value /
Math.Pow(1024, i)) +
" " + suffixes[i];
}
} return ThreeNonZeroDigits(value /
Math.Pow(1024, suffixes.Length - 1)) +
" " + suffixes[suffixes.Length - 1];
}
 
// Return the value formatted to include at most three
// non-zero digits and at most two digits after the
// decimal point. Examples:
// 1
// 123
// 12.3
// 1.23
// 0.12
private static string ThreeNonZeroDigits(double value)
{
if (value >= 100)
{
// No digits after the decimal.
return value.ToString("0,0");
}
else if (value >= 10)
{
// One digit after the decimal.
return value.ToString("0.0");
}
else
{
// Two digits after the decimal.
return value.ToString("0.00");
}
}

 

参考资料

扩展方法 (C# 编程指南) (Entrance on MSDN)

http://msdn.microsoft.com/zh-CN/library/bb383977

http://msdn.microsoft.com/zh-cn/magazine/cc163317.aspx

c# 扩展方法奇思妙用

http://www.cnblogs.com/ldp615/archive/2009/08/07/1541404.html

Conversion rules for Instance parameters and their impact

http://blogs.msdn.com/b/sreekarc/archive/2007/10/11/consequences-of-conversion-rules-for-instance-parameters.aspx

Extension methods Interoperability between languages

http://blogs.msdn.com/b/sreekarc/archive/2007/10/11/extension-methods-interoperability-between-languages.aspx

开源扩展项目

http://dnpextensions.codeplex.com/

C# 扩展方法集的更多相关文章

  1. MVC分页控件之二,为IQueryable定义一个扩展方法,直接反回PagedList<T>结果集(转)

    namespace Entity { public interface IPagedList { /// <summary> /// 记录数 /// </summary> in ...

  2. Mvc分页:为IQueryable定义一个扩展方法,直接反回PagedList<T>结果集

    namespace Entity{ public interface IPagedList { /// <summary> /// 记录数 /// </summary> int ...

  3. JS方法集

    数组方法集 Angela.array = { //# 数组方法 // index, 返回位置! 不存在则返回 -1: index: function (t, arr) { //# 返回当前值所在数组的 ...

  4. C#高级知识点概要(3) - 特性、自动属性、对象集合初始化器、扩展方法、Lambda表达式和Linq查询

    1.特性(Attributes) 特性(Attributes),MSDN的定义是:公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型.字段.方法 ...

  5. jQuery each、节点操作、动画演示、尺寸操作、扩展方法

    一.each 1.方式一:$.each(数组或者自定义对象,function(i,j){console.log(i,j)}) $.each(li,function(i,j){ console.log( ...

  6. artDialog学习之旅(二)之扩展方法详解

    名称 描述 核心方法 art.dialog.top 获取artDialog可用最高层window对象.这与直接使用window.top不同,它能排除artDialog对象不存在已经或者顶层页面为框架集 ...

  7. 3.4.1 使用过滤式扩展方法(P43-44)

    对IEnumerable<T>执行标准并且同样返回IEnumerable<T>的扩展方法,可以使用yield关键字对源数据中的项应用选择标准,已生成精简的结果集. public ...

  8. jQuery对象扩展方法(Extend)深度解析

    1.这几天在写自己的Js工具类库,所以在编写对象扩展方法,参考了jQuery的对象扩展方法,在编写该方法前,需要掌握js深拷贝和浅拷贝的相关知识,下面是jQuery3.2.1版本对象扩展方法的源码: ...

  9. C#对IQueryable<T>、IEnumerable<T>的扩展方法

    #region IQueryable<T>的扩展方法 #region 根据第三方条件是否为真是否执行指定条件的查询 /// <summary> /// 根据第三方条件是否为真是 ...

随机推荐

  1. Html 文档在线编辑器

    // //

  2. sqlserver 多库查询 sp_addlinkedserver使用方法(添加链接服务器)

    sqlserver 多库查询 sp_addlinkedserver使用方法(添加链接服务器) 我们日常使用SQL Server数据库时,经常遇到需要在实例Instance01中跨实例访问Instanc ...

  3. bzoj 1179[Apio2009]Atm (tarjan+spfa)

    题目 输入 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号.接下来N行,每行一 ...

  4. 完全卸载mysql步骤

    (1) 开始-MySQL-MySQL Server 5.1-MySQL Server Instance Config Wizard--->Remove Instance. (2)点击[开始]-- ...

  5. java web学习总结(十四) -------------------JSP原理

    一.什么是JSP? JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于,写jsp就像在写h ...

  6. Javascript 创建对象方法的总结

    最近看了一下<Javascript高级程序设计(第三版)>,这本书很多人都推荐,我也再次郑重推荐一下.看过之后总得总结一下吧,于是我选了这么一个主题分享给大家. 使用Javascript创 ...

  7. Code First :使用Entity. Framework编程(8) ----转发 收藏

    第8章 Code First将走向哪里? So far, this book has covered all of the Code First components that reached the ...

  8. 操作DOM

    操作dom一般是如下4个:更新:更新该DOM节点的内容,相当于更新了该DOM节点表示的HTML的内容:遍历:遍历该DOM节点下的子节点,以便进行进一步操作:添加:在该DOM节点下新增一个子节点,相当于 ...

  9. git mv 命令 移动或重命名

    最基本的代码: git mv file_old file_new 手册: NAME git-mv - Move or rename a file, a directory, or a symlink ...

  10. JavaScript中this指向的简单理解

    首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象(这句话有些问题,后面会解释为什么会有问题,虽然 ...