C# Extension Methods(C#类方法扩展)
使用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#类方法扩展)的更多相关文章
- C# -- 扩展方法的应用(Extension Methods)
当你有下面这样一个需求的时候,扩展方法就会起到作用:在项目中,类A需要添加功能,我们想到的就是在类A中添加公共方法,这个显而易见肯定可以,但是由于某种原因,你不能修改类A本身的代码,但是确实又需要增加 ...
- (转)C# -- 扩展方法的应用(Extension Methods)
本文转载自:http://blog.csdn.net/zxz414644665/article/details/9793205 当你有下面这样一个需求的时候,扩展方法就会起到作用:在项目中,类A需要添 ...
- Extension Methods(扩展方法)
在 OOPL 中,有静态方法.实例方法和虚方法,如下: public sealed class String { public static bool IsNullOrEmpty(st ...
- Extension Methods "点"函数方法 扩展方法
原文发布时间为:2011-03-25 -- 来源于本人的百度文章 [由搬家工具导入] http://msdn.microsoft.com/en-us/library/bb383977.aspx 条件: ...
- C# Extension Methods
In C#, extension methods enable you to add methods to existing class without creating a new derived ...
- 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 ...
- Extension Methods
Oftentimes you’ll find yourself using classes you can’t modify. Whether they’re basic data types or ...
- Top useful .Net extension methods
Special extension methods were released in C# 3.0. Developers have continuously been looking for way ...
- 重构改善既有代码设计--重构手法16:Introduce Foreign Method (引入外加函数)&& 重构手法17:Introduce Local Extension (引入本地扩展)
重构手法16:Introduce Foreign Method (引入外加函数)你需要为提供服务的类增加一个函数,但你无法修改这个类.在客户类中建立一个函数,并以第一参数形式传入一个服务类实例. 动机 ...
随机推荐
- HearthAgent A Hearthstone agent
http://www.intelligence.tuc.gr/~robots/ARCHIVE/2015w/Projects/LAB51326833/download.html The project ...
- C++ transform for_each
#include<iostream>#include<vector>#include <list>#include <algorithm>#includ ...
- linux简单命令8---用户登录查看命令
---------------------------------------------------------------------------------------------------- ...
- Java NIO学习笔记 三 散点/收集 和频道转换
Java NIO散点/收集 Java NIO带有内置的分散/收集支持.散点/收集是读取和写入渠道过程中使用的概念. 从通道散射读取是将数据读入多个缓冲区的读取操作.因此,数据可以从通道“散布”到多个缓 ...
- python图论包networks(最短路,最小生成树带包)
官方文档: https://networkx.github.io/documentation/networkx-1.10/reference/algorithms.html 最短路和最小生成树: im ...
- windows 下OPENSSL 生成秘钥和公钥的方法
1. 生成原始 RSA私钥文件 private_key.pem openssl genrsa -out private_key.pem 1024 2. 将原始 RSA私钥转换为 pkcs8格式 ope ...
- Clonezilla克隆还原系统
简介 Clonezilla是一个专门用来克隆磁盘驱动器的Linux发行版.它可以操作任何你所能想象到的文件系统类型.Clonezilla有两种版本:Live和SE.Live版本与Ubuntu的Live ...
- SpringBoot: 13.异常处理方式3(使用@ControllerAdvice+@ExceptionHandle注解)(转)
问题:使用@ExceptionHandle注解需要在每一个controller代码里面都添加异常处理,会咋成代码冗余 解决方法:新建一个全局异常处理类,添加@ControllerAdvice注解即可 ...
- Python3 Selenium自动化web测试 ==>FAQ:Unittest测试报告生成文件名加测试完成时间字符串
测试代码,虽然有点笨重,以后再修改: if __name__ == '__main__': report = os.path.join('D:/Python36/report/report.html' ...
- Python Elasticsearch
以下所用版本为Elasticsearch 7.2.0 1.安装 pip3 install elasticsearch -i https://pypi.tuna.tsinghua.edu.cn/simp ...