使用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. 重读APUE(9)-SIG_ERR、SIG_DFL、SIG_IGN定义无参数

    下面这几个函数定义,每次看到都会纠结一阵子,奇怪的是为什么没有参数? #define SIG_ERR (void (*)())-1 #define SIG_DFL (void (*)())0 #def ...

  2. nltk data 离线安装

    运行程序时发现如下错误: 在命令行下载速度太慢,因此需要离线安装: 按照:http://www.nltk.org/data.html 中manual installation的方法, 将包下载好后解压 ...

  3. Flutter移动电商实战 --(37)路由_Fluro引入和商品详细页建立

    https://github.com/theyakka/fluro pages/details_page.dart新建页面 使用路由 先添加路由插件的引用 fluro: ^1.4.0 如果网络上下载不 ...

  4. busybox中memdev的使用方法

    busybox中已经集成了devmem工具,你可以配置busybox即可. 在busybox的杂项中找到: CONFIG_USER_BUSYBOX_DEVMEM: devmem is a small ...

  5. Ubuntu 18.04安装arm-linux-gcc交叉编译器(超简单,附安装包下载地址)

    目前网上搜索发现,最多人安装的是4.4.3版本的: arm-linux-gcc-4.4.3.tar.gz下载地址:https://pan.baidu.com/s/1rAIBASIRZAXl-P1UOW ...

  6. 花椒直播基于golang的中台技术实践

    https://github.com/gopherchina/conference/blob/master/2019/2.7%20花椒直播基于golang的中台技术实践%20-%20周洋.pdf 花椒 ...

  7. kotlin之函数的范围和泛型函数

    kotlin 中函数可以定义为局部函数,成员函数以及扩展函数 局部函数:就是嵌套在函数内的函数 成员函数就是定义在类或者对象之内的函数 泛型函数就是函数可以带有泛型参数,可通过尖括号来指定

  8. 9Flutter GridView组件 以及动态GridView

    main.dart import 'package:flutter/material.dart'; import 'res/listData.dart'; /* GridView : 通过GridVi ...

  9. 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_09-webpack研究-webpack介绍

    使用vue.js开发大型应用需要使用webpack打包工具,本节研究webpack的使用方法. 1.3.1 webpack介绍 Webpack 是一个前端资源的打包工具,它可以将js.image.cs ...

  10. CodeIgniter问题:Unable to load the requested file: .php

    调试时出现 Unable to load the requested file: .php, 后来排查到是模板渲染的问题,view函数的参数没接收到,修改后就好了.