语法注意点

  • 可以使用扩展方法来扩展类或接口。
  • 不能重写扩展方法。
  • 扩展方法只能在非嵌套、非泛型静态类内部定义。
  • 扩展方法必须定义在静态类中。
  • 扩展方法的第一个参数的类型用于指定被扩展的类型,它限制该扩展方法只能作用于该类型。
  • 扩展方法的第一个参数必须带有 "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. Openfire 4.1.0

    http://www.igniterealtime.org/downloads/index.jsp 服务器端口 接口 端口   类型 描述 所有的地址 5222 客户端到服务器 客户端使用标准端口连接 ...

  2. centos6.x 安装pylucene (20161027改)

    一.说明 安装环境 centos6.6 (64位) python2.7.10 (升级系统默认python版本的方法参见在CentOS 6.5上安装python2.7) 约定 工作目录假定为当前用户的H ...

  3. html+css+js 模拟win7桌面

    不贴代码了,直接上传附件. 附件下载: win7desktop

  4. 鼠标移到导航上面 当前的LI变色 处于当前的位置

    鼠标移到导航上面 当前的LI变色 处于当前的位置,广泛应用于当前导航. 点击这里查看效果 以下是源代码: <html> <head> <meta http-equiv=& ...

  5. ASP.NET MVC 了解FileResult的本质

    FileResult是一个基于文件的ActionResult,利用FileResult我们可以很容易地将从某个物理文件的内容响应给客户端.ASP.NET MVC定义了三个具体的FileResult,分 ...

  6. 从零开始学Python第六周:面向对象基础(需修改)

    标签(空格分隔): 面向对象 一,面向对象基础 (1)面向对象概述 面向过程:根据业务逻辑从上到下写代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类 ...

  7. java实现链表

    单链表 package com.voole.linkedlist; public class Test { public static void main(String[] args) { Linke ...

  8. 《More Effective C#》读书笔记

    <More Effective C#>这本书,大概是四年前看完的,但只整理了一部分读书笔记,后面有时间的话,会陆续补充的. More Effective C# :使用泛型 More Eff ...

  9. 这个jQuery导航菜单怎么样

    效果体验:http://keleyi.com/keleyi/phtml/jqtexiao/39.htm HTML文件代码: <!DOCTYPE html> <html xmlns=& ...

  10. 爬虫的入门以及scrapy

    一.简介 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用的名字还有蚂蚁.自动索引.模拟 ...