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 (引入外加函数)你需要为提供服务的类增加一个函数,但你无法修改这个类.在客户类中建立一个函数,并以第一参数形式传入一个服务类实例. 动机 ...
随机推荐
- [go]ini配置文件解析
// config.ini [app] server.port = 8080 name = resk enabled = false time = 10s ;我是一个注释 #mysql数据库配置 [m ...
- [activemq]+spring的使用
一.生产端(Producer) applicationContext.xml中的配置: <?xml version="1.0" encoding="UTF-8&qu ...
- Redis查询_Tips
基础知识——介绍 Redis简介 REmote Dictionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统. Redis是一个完全 ...
- Python字符串逐字符或逐词反转方法
Python字符串逐字符或逐词反转方法 这篇文章主要介绍了Python字符串逐字符或逐词反转方法,本文对逐字符或逐词分别给出两种方法,需要的朋友可以参考下 目的 把字符串逐字符或逐词反转过来,这个蛮有 ...
- ubuntu 安装的一些软件
https://github.com/wszqkzqk/deepin-wine-ubuntu 14 版本的ubuntu 安装发生问题; 16 18 的都可以安装成功微信;
- [C++]数据结构:线性表之顺序表
1 顺序表 ADT + Status InitList(SeqList &L) 初始化顺序表 + void printList(SeqList L) 遍历顺序表 + int ListLengt ...
- 利用官方的ucosiii包中测试板的工程移植到属于自己的开发板(stmf103ZE)上
ucosIII官方下载地址:https://www.micrium.com 第一:是不是ucosIII:第二,工具链是不是keil(我用的是keil,如何用的是IAR就选有IAR的):第三MCU是不是 ...
- 【Matlab开发】matlab中bar绘图设置与各种距离度量
[Matlab开发]matlab中bar绘图设置与各种距离度量 标签(空格分隔): [Matlab开发] [机器学习] 声明:引用请注明出处http://blog.csdn.net/lg1259156 ...
- codevs 1039:数的划分
http://codevs.cn/problem/1039/ 题目描述 Description将整数n分成k份,且每份不能为空,任意两种划分方案不能相同(不考虑顺序).例如:n=7,k=3,下面三种划 ...
- Linux 系统信息查询大全
Linux常用系统命令 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /etc/redhat-release ...