C# Extension Methods
In C#, extension methods enable you to add methods to existing class without creating a new derived class.
Extension methods 要求:
- Define a static class to contain extension method. This class must be visible to client code.
- Implement the extension method like a static method, but add "this" modifier before the first parameter.
- The first parameter specifies the type that this extension method operates on.
using System.Globalization;
using System.Threading; namespace Library
{
public static class StringExtensions
{
//static method
//public static string ConvertToTitleCase(this string source)
//extension method
public static string ConvertToTitleCase(this string source)
{
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo; return textInfo.ToTitleCase(source);
}
}
}
Extension methods call:
Call extension method like extension method is an instance method on the type.
namespace Library_Simple
{
//Import extension method namespace
using Library;
class Program
{
static void Main(String[] args)
{
string source = "the return of the king";
string extected = "The Return Of The King"; //static method
//string result = StringExtensions.ConvertToTitleCase(source);
//extension method
string result = source.ConvertToTitleCase(); Assert.IsNotNull(result);
Assert.AreEqual(expected, result);
}
}
}
C# Extension Methods的更多相关文章
- 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 ...
- C# -- 扩展方法的应用(Extension Methods)
当你有下面这样一个需求的时候,扩展方法就会起到作用:在项目中,类A需要添加功能,我们想到的就是在类A中添加公共方法,这个显而易见肯定可以,但是由于某种原因,你不能修改类A本身的代码,但是确实又需要增加 ...
- Top useful .Net extension methods
Special extension methods were released in C# 3.0. Developers have continuously been looking for way ...
- (转)C# -- 扩展方法的应用(Extension Methods)
本文转载自:http://blog.csdn.net/zxz414644665/article/details/9793205 当你有下面这样一个需求的时候,扩展方法就会起到作用:在项目中,类A需要添 ...
- Extension Methods "点"函数方法 扩展方法
原文发布时间为:2011-03-25 -- 来源于本人的百度文章 [由搬家工具导入] http://msdn.microsoft.com/en-us/library/bb383977.aspx 条件: ...
- Extension Methods(扩展方法)
在 OOPL 中,有静态方法.实例方法和虚方法,如下: public sealed class String { public static bool IsNullOrEmpty(st ...
- Extension Methods (C# Programming Guide)
https://msdn.microsoft.com/en-us//library/bb383977.aspx private static void Dump(this ArraySegment&l ...
- C# Extension Methods(C#类方法扩展)
使用Extension methods 可以在已有的类型(types)中添加方法(Methods),而无需通过增加一种新的类型或修改已有的类型. 比如说,想要给string类型增加一个PrintStr ...
随机推荐
- 【转】详解Java正则表达式语法
(转自: http://www.jb51.net/article/76354.htm) 这篇文章主要介绍了Java正则表达式语法,包括常用正则表达式.匹配验证-验证Email是否正确以及字符串中查询字 ...
- 利用ipython实现多线程
多线程来批量化处理数据的时候希望简单的应用,使用ipython会很简单 参考这里
- [leetcode] 题型整理之查找
1. 普通的二分法查找查找等于target的数字 2. 还可以查找小于target的数字中最小的数字和大于target的数字中最大的数字 由于新的查找结果总是比旧的查找结果更接近于target,因此只 ...
- 单色半透明-兼容IE7
background: #000; width: 100%;height: 100%; filter: alpha(opacity=30); opacity: 0.3;
- iOS UIView动画效果 学习笔记
//启动页动画 UIImageView *launchScreen = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds]; ...
- 【转】oracle 中随机取一条记录的两种方法
oracle 中随机取一条记录的两种方法 V_COUNT INT:=0; V_NUM INT :=0; 1:TBL_MYTABLE 表中要有一个值连续且唯一的列FID BEGIN SELECT COU ...
- Eclipse中快速删除空行
1.在要编辑的文档中 "Ctrl"+"F",弹出搜索框: 2.在Find文本框中输入正则表达式:^\s*\n 3.勾选正则表达式选项: 4.Find和替换所有.
- 《Invert》开发日志05:终止
今天终于看了久闻大名的<独立游戏大电影>,然后我就做了一个坑爹的决定:终止“Invert”项目的开发.没错,在还没正式开工之前,我就决定停掉这个项目,而且是永久终止.做这个决定并不是因为觉 ...
- 【Cocos2d-x游戏开发】细数Cocos2d-x开发中那些常用的C++11知识
自从Cocos2d-x3.0开始,Cocos2dx就正式的使用了C++11标准.C++11简洁方便的特性使程序的可拓展性和可维护性大大提高,也提高了代码的书写速度. 下面我们就来一起学习一下Cocos ...
- Markdown工具的使用
Markdown 工具的使用 基本操作 CMD-N 建立一个新的工作表 CMD-SHIFT-N 建立一个新的组 CMD-CTRL-N 建立一个新的过滤器 项目总是会被创建在当前所选的附近 工作表会被创 ...