使用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. AddLayer和AddTag

    using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; pu ...

  2. Qt之编译MySQL数据库驱动(MSVC)

    Qt之编译MySQL数据库驱动(MSVC) (2013-12-13 20:24:46) 转载▼ 标签: qt mysql qmysql qt编译mysql qt之msvc编译mysql 分类: Qt ...

  3. QT 自定义消息

    #define TEST_EVENT QEvent::User + 100   class CVxActuatorMain : public QMainWindow {   protected:    ...

  4. 图像质量评价-NQM和WPSNR

    王保全. 基于混合专家模型的快速图像超分辨率方法研究与实现[D]. 2015. PSNR 和SSIM 在有时候并不能很确切的表示图像质量 标准,该论文中根据一定量的人为的感知评分作为参考,用斯皮尔曼等 ...

  5. Windows10 64位 安装 Postgresql 数据库

    1,下载Postgresql 10.7 版本,下载地址   https://www.enterprisedb.com/downloads/postgres-postgresql-downloads 2 ...

  6. using代码性能影响?

    Q:using当作命名空间?A:对代码编译有影响,对代码执行无任何影响:https://www.cnblogs.com/Interkey/p/UsingNameSpace.html Q:using用来 ...

  7. windows进入指定目录

    1.进入cmd 2.输入盘符比如:E: 3.切换目录 cd E:\progect\Firstdjango 实例:

  8. 使用apache commons csv解析del(类似csv)格式文件

    del格式类似csv,我这里的测试文件test.del是从DB2数据库中导出来的一组数据. apache commons csv的maven坐标为: <!-- https://mvnreposi ...

  9. 【SQL】在数据库中发起http请求的小改进

    市面上常见的是用MSXML2.ServerXMLHTTP这个类,但这个类在发起异步请求时并不可靠,就是当send后并不一定会发出这个请求.这里推荐改用Microsoft.XMLHTTP,如果只是简单的 ...

  10. 如何做一个项目v2.ppt

    链接:https://pan.baidu.com/s/159GQsYK9BcQad3h1CyVUYg 提取码:ivcj 复制这段内容后打开百度网盘手机App,操作更方便哦