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. 哈,我自己翻译的小书,马上就完成了,是讲用python处理大数据框架hadoop,spark的

    花了一些时间, 但感觉很值得. Big Data, MapReduce, Hadoop, and Spark with Python Master Big Data Analytics and Dat ...

  2. [LeetCode] Reverse Linked List

    Reverse a singly linked list. 这题因为palindrome linked list 的时候需要就顺便做了一下.利用三个指针:prev, now, next 相互倒腾就行. ...

  3. Failed creating java D:/jre6/bin/client/jvm.dll

    Failed creating java D:/jre6/bin/client/jvm.dll 标记一下 坑爹啊! 我特么装了一个64位的eclipse 结果报错 目录下确实有这个文件. 我想说  6 ...

  4. 注解:【有连接表的】Hibernate双向1->N关联(仅N端控制关联关系)

    Person与Address关联:双向1->N,[有连接表的],N端控制关联关系 Person.java package org.crazyit.app.domain; import java. ...

  5. LINUX 查看当前系统的负载情况

    uptime linux uptime命令主要用于获取主机运行时间和查询linux系统负载等信息. eg: # uptime 02:03:50 up 126 days, 12:57, 2 users, ...

  6. 几种经典排序算法的R语言描述

    1.数据准备 # 测试数组 vector = c(,,,,,,,,,,,,,,) vector ## [] 2.R语言内置排序函数 在R中和排序相关的函数主要有三个:sort(),rank(),ord ...

  7. 25 Killer Actions to Boost Your Self-Confidence

    25 Killer Actions to Boost Your Self-Confidence Once we believe in ourselves, we can risk curiosity, ...

  8. SecureCRT中python脚本编写

    SecureCRT中python脚本编写学习指南 SecureCRT python 引言 在测试网络设备中,通常使用脚本对设备端进行配置和测试以及维护:对于PE设备的测试维护人员来说使用较多是Secu ...

  9. freecodecamp记录

    来源:https://www.freecodecamp.cn 如果需要填充文本来检查排版效果,网上有自动生成器,乱文生成器:此外Microoft Word中有一个函数能够自动生成每段20句的6段填充文 ...

  10. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...