In C#, extension methods enable you to add methods to existing class without creating a new derived class.

Extension methods 要求:

  1. Define a static class to contain extension method. This class must be visible to client code.
  2. Implement the extension method like a static method, but add "this" modifier before the first parameter.
  3. 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的更多相关文章

  1. 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 ...

  2. Extension Methods

    Oftentimes you’ll find yourself using classes you can’t modify. Whether they’re basic data types or ...

  3. C# -- 扩展方法的应用(Extension Methods)

    当你有下面这样一个需求的时候,扩展方法就会起到作用:在项目中,类A需要添加功能,我们想到的就是在类A中添加公共方法,这个显而易见肯定可以,但是由于某种原因,你不能修改类A本身的代码,但是确实又需要增加 ...

  4. Top useful .Net extension methods

    Special extension methods were released in C# 3.0. Developers have continuously been looking for way ...

  5. (转)C# -- 扩展方法的应用(Extension Methods)

    本文转载自:http://blog.csdn.net/zxz414644665/article/details/9793205 当你有下面这样一个需求的时候,扩展方法就会起到作用:在项目中,类A需要添 ...

  6. Extension Methods "点"函数方法 扩展方法

    原文发布时间为:2011-03-25 -- 来源于本人的百度文章 [由搬家工具导入] http://msdn.microsoft.com/en-us/library/bb383977.aspx 条件: ...

  7. Extension Methods(扩展方法)

    在 OOPL 中,有静态方法.实例方法和虚方法,如下:   public sealed class String {      public static bool  IsNullOrEmpty(st ...

  8. Extension Methods (C# Programming Guide)

    https://msdn.microsoft.com/en-us//library/bb383977.aspx private static void Dump(this ArraySegment&l ...

  9. C# Extension Methods(C#类方法扩展)

    使用Extension methods 可以在已有的类型(types)中添加方法(Methods),而无需通过增加一种新的类型或修改已有的类型. 比如说,想要给string类型增加一个PrintStr ...

随机推荐

  1. xcode 常见错误报错问题!

    1:module  file '/Users/shaka/Library/Developer/Xcode/DerivedData/ModuleCache/92IKIZLYISUT/Darwin-2OA ...

  2. code first 创建和初始化数据库

    1.前言 Code First是Entity Framework提供的一种新的编程模型.通过Code First我们可以在还没有建立数据库的情况下就开始编码,然后通过代码对象来生成数据库.当然我们在实 ...

  3. 基于Struts2CRUD的质量属性

    基于struts2框架开发的<学生管理系统>的质量属性 我们经常重新设计系统,可能不是因为该系统在功能上有缺陷,而是由于:系统运行速度太慢.系统容易受到外界攻击.用另外的一句话说:我们修改 ...

  4. Linux常用命令学习6---(vim的使用)

    先说说我,我使用了这么久的vim,但是完全没有将vim的功能完全利用到,无非就是使用了编辑(i).保存(:w).退出(:q).等简单的编辑,命令,以及NerdTree这一个插件,所以在这里需要重新学习 ...

  5. Jetty使用教程(四:24-27)—Jetty开发指南

    二十四.处理器(Handler ) 24.1 编写一个常用的Handler Jetty的Handler组件用来处理接收到的请求. 很多使用者不需要编写Jetty的Handler ,而是通过使用Serv ...

  6. UVA 10780 Again Prime No Time.(数学)

    给定两个整数m和n,求最大的k使得m^k是n!的约数 对m质因子分解,然后使用勒让德定理求得n!包含的质数p的阶数,min(b[i] / a[i])即为结果k, 若为0无解 #include<c ...

  7. windows 10 设置

    精简应用 邮件和日历: Get-AppxPackage *communi* | Remove-AppxPackage 新闻: Get-AppxPackage *bing* | Remove-AppxP ...

  8. 08 Servlet

    Servlet技术          * Servlet开发动态的Web资源的技术.                       * Servlet技术               * 在javax. ...

  9. keyCode,charCode,which

    1.触发顺序keydown keypress keyup,但keypress事件只有输入相关按键才会触发,功能按键不会触发keypress事件(keypress事件有个额外的charCode属性) 2 ...

  10. tornado 学习笔记8 模板以及UI

          Tornado 包含一个简单.快速而且灵活的模板语言.       Tornado同样可以使用任何其他的python模板语言,虽然没有集成这些模板语言进RequestHandler.ren ...