使用Extension methods 可以在已有的类型(types)中添加方法(Methods),而无需通过增加一种新的类型或修改已有的类型。

比如说,想要给string类型增加一个PrintStrLength()方法,打印出字符串的长度。使用Extension methods可以不需要去修改string类型而实现这一方法。

"hello".PrintStrLength();         //打印出 length of hello is 5

 using System;

 namespace ExtensionDemo
{
using Extensions;
class Program
{
static void Main(string[] args)
{
"hello".PrintStrLength();
         "extension".PrintStrLength();
}
}
}
namespace Extensions
{
// Extension method must be defined in a non-generic static class
static class StrExtensions
{
public static void PrintStrLength(this string str)
{
Console.WriteLine($"length of \"{str}\" is {str.Length}");
}
}
}

上面的Demo的运行结果是

length of "hello" is 5
length of "extension" is 9

注意几点:

  • Extension methods 扩展方法必须定义在一个静态类中
  • Extension methods 是一种特殊的静态方法,在被扩展的类型调用时要像实例方法一样调用。 ("hello".PrintStrLength();)
  • 扩展方法的声明   使用this关键字 后面跟着要操作的对象类型
    public static void PrintStrLength(this string str)
  • 扩展方法的使用: 使用扩展方法时只需先使用using导入扩展方法所在的名字空间(namespace)。在VS中输入扩展方法时intellisense会提示(extension)
  • C#不能扩展静态类(如System.Convert)

LINQ查询就是一种常用的对System.Collections.IEnumerable类型的扩展方法(GroupBy, OrderBy, Average方法)。使用时用 Using System.Linq; 语句导入Linq名字空间。

C# Extension Methods(C#类方法扩展)的更多相关文章

  1. C# -- 扩展方法的应用(Extension Methods)

    当你有下面这样一个需求的时候,扩展方法就会起到作用:在项目中,类A需要添加功能,我们想到的就是在类A中添加公共方法,这个显而易见肯定可以,但是由于某种原因,你不能修改类A本身的代码,但是确实又需要增加 ...

  2. (转)C# -- 扩展方法的应用(Extension Methods)

    本文转载自:http://blog.csdn.net/zxz414644665/article/details/9793205 当你有下面这样一个需求的时候,扩展方法就会起到作用:在项目中,类A需要添 ...

  3. Extension Methods(扩展方法)

    在 OOPL 中,有静态方法.实例方法和虚方法,如下:   public sealed class String {      public static bool  IsNullOrEmpty(st ...

  4. Extension Methods "点"函数方法 扩展方法

    原文发布时间为:2011-03-25 -- 来源于本人的百度文章 [由搬家工具导入] http://msdn.microsoft.com/en-us/library/bb383977.aspx 条件: ...

  5. C# Extension Methods

    In C#, extension methods enable you to add methods to existing class without creating a new derived ...

  6. AX7: HOW TO USE CLASS EXTENSION METHODS

    AX7: HOW TO USE CLASS EXTENSION METHODS   To create new methods on a standard AX class without custo ...

  7. Extension Methods

    Oftentimes you’ll find yourself using classes you can’t modify. Whether they’re basic data types or ...

  8. Top useful .Net extension methods

    Special extension methods were released in C# 3.0. Developers have continuously been looking for way ...

  9. 重构改善既有代码设计--重构手法16:Introduce Foreign Method (引入外加函数)&& 重构手法17:Introduce Local Extension (引入本地扩展)

    重构手法16:Introduce Foreign Method (引入外加函数)你需要为提供服务的类增加一个函数,但你无法修改这个类.在客户类中建立一个函数,并以第一参数形式传入一个服务类实例. 动机 ...

随机推荐

  1. HTTP之基本认证机制

    1. 认证 1.1 HTTP 的质询/响应认证框架 HTTP 提供了一个原生的质询/响应(challenge/response)框架,简化了对用户的认证过程. HTTP 的认证模型如下图所示: Web ...

  2. 如何配置WAMP环境(主要是Apache与PHP)

    1.配置Apache 1.编辑Apache配置文件---http.conf(位于安装目录下的conf子目录内): 2.修改Document Root 和 Directory选项,这是修改Apache的 ...

  3. 转贴:PLSQL中 commit 和 rollback 的区别

    PLSQL中 commit 和 rollback 的区别 原文链接:https://blog.csdn.net/jerrytomcat/article/details/82250915 一. comm ...

  4. [转][C#]基础连接已经关闭 未能为 SSL/TLS 安全通道建立信任关系

    来自:https://www.cnblogs.com/waw/p/8286608.html 代码部分: static TestApplication(){ ServicePointManager.Se ...

  5. SqlServer2008数据库的备份与还原

    1.先是备份数据 1.1.登录sql server management studio 1.2.选中需要备份数据库,右击鼠标,如下图: 1.3.点击备份之后,如下图; 2.数据还原准备 ps: 在开始 ...

  6. 标签 <i>

    <i>的使用 效果图

  7. getchwd() 函数返回当前工作目录。

    getchwd() 函数返回当前工作目录.

  8. 三小时攻克 Kubernetes!

    我保证本文是最详尽的 Kubernetes 技术文档,从我在后台排版了这么漫长的时间就能看出来.废话不多说——牢牢占据容器技术统治地位的 Kubernetes,其重要性想必不言而喻. 以下为译文: 为 ...

  9. Mysql按照设计顺序获得某个表的字段名称,字段类型,字段描述!!!!!

    编写sql语句 select column_name,data_type ,column_comment from information_schema.columns where table_nam ...

  10. 最新 汽车之家java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.汽车之家等10家互联网公司的校招Offer,因为某些自身原因最终选择了汽车之家.6.7月主要是做系统复习.项目复盘.Leet ...