C# Extension Methods

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.
 1 using System.Globalization;
 2 using System.Threading;
 3
 4 namespace Library
 5 {
 6     public static class StringExtensions
 7     {
 8         //static method
 9         //public static string ConvertToTitleCase(this string source)
10         //extension method
11         public static string ConvertToTitleCase(this string source)
12         {
13             CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
14             TextInfo textInfo = cultureInfo.TextInfo;
15
16             return textInfo.ToTitleCase(source);
17         }
18     }
19 }

Extension methods call:

Call extension method  like extension method is an instance method on the type.

 1 namespace Library_Simple
 2 {
 3     //Import extension method namespace
 4     using Library;
 5     class Program
 6     {
 7         static void Main(String[] args)
 8         {
 9             string source = "the return of the king";
10             string extected = "The Return Of The King";
11
12             //static method
13             //string result = StringExtensions.ConvertToTitleCase(source);
14             //extension method
15             string result = source.ConvertToTitleCase();
16
17             Assert.IsNotNull(result);
18             Assert.AreEqual(expected, result);
19         }
20     }
21 }
时间: 2024-10-05 15:00:25

C# Extension Methods的相关文章

Extension Methods

Oftentimes you’ll find yourself using classes you can’t modify. Whether they’re basic data types or part of an existing framework, you’re stuck with the functions that are provided. That being said, C# provides a nifty trick to appending functions to

Extension Methods(扩展方法)

在 OOPL 中,有静态方法.实例方法和虚方法,如下: public sealed class String { public static bool  IsNullOrEmpty(string s) { // ... } public string Replace(string old, string new) { // ... } } public abstract class Stream { public virtual void WriteByte(byte value) { // .

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

本文转载自:http://blog.csdn.net/zxz414644665/article/details/9793205 当你有下面这样一个需求的时候,扩展方法就会起到作用:在项目中,类A需要添加功能,我们想到的就是在类A中添加公共方法,这个显而易见肯定可以,但是由于某种原因,你不能修改类A本身的代码,但是确实又需要增加功能到类A中去,怎么办? 这个时候扩展方法(Extension Methods)就会帮助你完成上述功能了.现在举例如下: 类A:简单起见,类A中只有一个自己的方法. usi

Unity3d的C#扩展方法Extension methods应用吧

扩展方法的条件: 必须声明为静态类 必须声明为静态方法 方法的第一个参数为this 首先来扩展Unity中的协程Coroutine, using UnityEngine; using System.Collections; using System; /// <summary> /// a set of extension methods meant help with common coroutine cases. Example : /// <code> /// void On

Top useful .Net extension methods

Special extension methods were released in C# 3.0. Developers have continuously been looking for ways to extend classes to every coding and got top most preferable extension methods for .net development.Is there a class that just doesn't do enough fo

对接口运用扩展方法 Applying Extension Methods to an Interface 精通ASP-NET-MVC-5-弗瑞曼 Listing 4-15

创建过滤扩展方法 Creating Filtering Extension Methods 精通ASP-NET-MVC-5-弗瑞曼 Listing 4-17

正式发布开源的扩展方法库FSLib.Extension (.NET3.5/4/4.5)

其实这个扩展方法库已经发布有段时间了,没有正式发布过.今天整理了一下文档,顺便搞个文档,正式发布吧. 简介 .NET在3.5开始引入的编译器支持扩展方法,允许为现有的对象类型在不改变其原本定义的情况下添加额外的方法. 因此便有了本扩展方法库.本扩展方法库基本上扩展的都是.NET原有的对象,尤其是String对象. 添加本扩展方法库的引用库后,一般不需要特定声明即可使用,大多为现有对象的快捷操作. 安装方法 你有三种方式可以使用本库: 直接使用包管理器(Nuget Package Manager)

9.Methods(二)

4.Operator Overload Methods allow a type to define how operators should manipulate instances of the type. 1.The CLR doesn’t know anything about operator overloading because it doesn’t even know what an operator is. programming language defines what e