Extension Method - c#3.0
Extension Method能够让你为一个已经存在的类添加方法,而不用去创建一个子类继承它。
具体做法:
- 定义一个静态的类来包含这个扩展方法。
- 定义扩展方法,必须是静态的,该方法的第一个参数用来指定是扩展哪个类的方法,
并且前面要加this关键字。
- 在想要调用扩展方法的类中,用using引入该类的命名空间就可以了。
下面是例子:
定义一个类来扩展string类:
namespace MyExtension
{
//Extension methods must be defined in a static class
public static class StringExtension
{
// This is the extension method.
// The first parameter takes the "this" modifier
// and specifies the type for which the method is defined.
public static int WordCount(this String str, string msg)
{
Console.WriteLine(msg);
return str.Split(new char[] { ' ', ',', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
}
}
}
在调用的类中用using MyExtension;先引入名空间,然后就可以用了:
private void Form1_Load(object sender, EventArgs e)
{
string str = "I come from China, and you? Nice to meet you.";
int count = str.WordCount("hello world");
// "hello world" 传给msg参数
}
转载于:https://www.cnblogs.com/bear831204/archive/2008/11/10/1330675.html
Extension Method - c#3.0相关推荐
- [译文]c#扩展方法(Extension Method In C#)
原文链接: https://www.codeproject.com/Tips/709310/Extension-Method-In-Csharp 介绍 扩展方法是C# 3.0引入的新特性.扩展方法使你 ...
- Extension Method (확장 메서드)
확장 메서드는 C# 3.0에서 새로 추가된 기능 입니다. 확장 메서드 (Extension Method)란 타입(Type)이 가지는 맴버에 사용자가 구현한 메서드를 추가 시키는 것입 ...
- 深入理解C# 3.x的新特性(2):Extension Method[下篇]
四.Extension Method的本质 通过上面一节的介绍,我们知道了在C#中如何去定义一个Extension Method:它是定义在一个Static class中的.第一个Parameter标 ...
- C# Note21: 扩展方法(Extension Method)及其应用
前言 今天在开会时提到的一个概念,入职3个多月多注重在项目中使用C#的编程知识,一直没有很认真地过一遍C#的全部语法,当我们新人被问及是否了解Extension Method时,一时之间竟不能很通俗准 ...
- Unity学习笔记--Extension Method 扩展方法
引言 最近跟着麦克学习Unity的时候,学习到了类的扩展方法.比如你现在有一个已有的类,你想给它加一个方法,当然如果是你自己写的类可以直接在这个方法里面写,但是如果是Unity自带的类,我们不可能直接 ...
- Unsatisfied dependency expressed through method ‘redisTemplate‘ parameter 0;
原因分析: Error starting ApplicationContext. To display the auto-configuration report re-run your applic ...
- Magento 产品推荐 extension Featured products 2.0 – revamped!
Platform: Community Platform: Community Platform: Enterprise Compatible with: 1.6, 1.6.1, 1.6.2.0, 1 ...
- authentication method mysql 8.0查询_mysql 索引整理
一.MySQL 索引 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更 ...
- Method not found: '!!0[] System.Array.Empty()'.
错误原因:程序里面没有可调用的方法(程序使用的是 .NET Framework 4.6,但是你自己的系统里面使用的不是 4.6版本) 解决方法:1.安装window sp1 ,下载地址是:https ...
最新文章
- 排序算法四:归并排序
- python isinstance函数
- 使用sklearn加载公共数据集、内存数据与CSV文件
- docker之container
- jquery 使用方法
- 使用栈实现队列 Implement Queue using Stacks
- nifi将hive同步到oracle,NiFi使用总结 一 hive到hive的PutHiveStreaming processor和SelectHiveQL...
- 关闭vue中的eslint校验
- 工具的使用——Photoshop
- 环境数据-全球中国土壤数据资源下载和提取教程
- presscad图层LIsp_PressCADLisp程序说明
- html在按钮中加图片,在html里面怎么为一个按钮添加图片
- 如何在linux下批量压缩图片
- vue 打包出现的问题解决
- java 地牢猎手,地牢猎手5新手必看速成攻略
- 台式计算机用u盘给电脑安装系统,台式机用u盘装系统,教您用u盘装系统给台式机...
- 2023湖南省中职网络安全任务书
- macbook不能进系统 备份数据_U盘装系统,系统分区备份,万兴数据恢复,介绍几款好用的系统软件...
- 华为C8815 默认logcat信息
- devexpress表格控件gridcontrol设置隔行变色、焦点行颜色、设置(改变)显示值、固定列不移动(附源码)...
热门文章
- 照片看3秒就销毁的软件_3.9秒破百,比亚迪汉EV到店实拍:实车比照片更好看
- 百度下mysql卸载_如何把Mysql卸载干净(亲测有效)
- eclipse java jsp_Javaweb学习笔记4 使用Eclipse快速开发JSP
- AD中如何查看快捷键
- python基础——lambda函数
- dcmtk 3.6.0 error C1083: Cannot open include file: 'iostream.h' 解决办法
- Qt TCP协议 传输简单字符串实例
- argv、lambda、filter、map、reduce
- Hibernate Search 6.0.0.Alpha3 发布,数据检索框架
- Web安全漏洞之SSRF