https://msdn.microsoft.com/en-us//library/bb383977.aspx

private static void Dump(this ArraySegment<byte> segment)
{
string output = string.Join(",", segment.Select(x => x.ToString()));
Console.WriteLine(output);
}

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.

For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

The most common extension methods are the LINQ standard query operators that add query functionality to the existingSystem.Collections.IEnumerable and System.Collections.Generic.IEnumerable<T> types.

To use the standard query operators, first bring them into scope范围 with a using System.Linq directive.

Then any type that implements IEnumerable<T> appears to have instance methods such asGroupBy<TSource, TKey>OrderBy<TSource, TKey>Average, and so on.

You can see these additional methods in IntelliSense statement completion when you type "dot" after an instance of an IEnumerable<T> type such as List<T> or Array.

The following example shows how to call the standard query operator OrderBy method on an array of integers.

The expression in parentheses圆括号 is a lambda expression.

Many standard query operators take lambda expressions as parameters, but this is not a requirement for extension methods.

For more information, see Lambda Expressions (C# Programming Guide).

class ExtensionMethods2
{ static void Main()
{
int[] ints = { , , , , , };
var result = ints.OrderBy(g => g);
foreach (var i in result)
{
System.Console.Write(i + " ");
}
}
}

Extension methods are defined as static methods but are called by using instance method syntax.

Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier.

Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.

The following example shows an extension method defined for the System.String class.

Note that it is defined inside a non-nested, non-generic static class:

namespace ExtensionMethods
{
public static class MyExtensions
{
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' },
StringSplitOptions.RemoveEmptyEntries).Length;
}
}
}

The WordCount extension method can be brought into scope with this using directive:

using ExtensionMethods;

And it can be called from an application by using this syntax:

string s = "Hello Extension Methods";
int i = s.WordCount();

In your code you invoke the extension method with instance method syntax.

However, the intermediate language (IL) generated by the compiler translates your code into a call on the static method.

Therefore, the principle of encapsulation is not really being violated违反.

In fact, extension methods cannot access private variables in the type they are extending.

For more information, see How to: Implement and Call a Custom Extension Method (C# Programming Guide).

In general, you will probably be calling extension methods far more often than implementing your own.

Because extension methods are called by using instance method syntax, no special knowledge is required to use them from client code.

To enable extension methods for a particular type, just add a using directive for the namespace in which the methods are defined.

For example, to use the standard query operators, add this using directive to your code:

using System.Linq;

(You may also have to add a reference to System.Core.dll.)

You will notice that the standard query operators now appear in IntelliSense as additional methods available for most IEnumerable<T> types.

Note:

Although standard query operators do not appear in IntelliSense for String, they are still available.

最新文章

  1. httpCookie与Cookie安全
  2. 十一个行为模式之备忘录模式(Memento Pattern)
  3. java反射机制深入详解
  4. C# WebClient 使用http免费代理。
  5. Eclipse+Mingw+Boost 环境搭建
  6. MSSQL 查询分组前N条记录
  7. BZOJ 3569 DZY Loves Chinese II
  8. Linux运维不可不知的性能监控和调试工具
  9. nodejs学习笔记_nodejs和PHP在基础架构上的差别--共享状态的并发
  10. CentOS下架设VNC服务器
  11. js阻止表单提交的两种方法
  12. ORA-00918: 未明确定义列
  13. jenkins+maven+git持续集成部署问题总结
  14. 从发布订阅模式到redux(一)
  15. 【Vue】中 $attrs 中的使用方法
  16. 线程的信号量Semaphore
  17. 树莓派安装vnc server并设置自启动
  18. Boost中的智能指针(转)
  19. bootstrap3文章
  20. [leetcode.com]算法题目 - Gray Code

热门文章

  1. Linux(centOS7.2)+node+express初体验
  2. js 学习笔记---BOM
  3. Spring Boot项目中使用 TrueLicense 生成和验证License(服务器许可)
  4. vue03 axios
  5. Django-----中间件Cookie
  6. odoo 权限杂记
  7. Python那点事
  8. Django——3 模板路径 模板变量 常用过滤器 静态文件的使用
  9. ajax学习----json,前后端交互,ajax
  10. 【codeforces 527D】Clique Problem