Extension Methods (C# Programming Guide)
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.
Extension Methods (C# Programming Guide)的更多相关文章
- Interfaces (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/ms173156.aspx An interface contains definitions for a group ...
- Versioning with the Override and New Keywords (C# Programming Guide)
The C# language is designed so that versioning between base and derived classes in different librari ...
- Polymorphism (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/ms173152.aspx Polymorphism is often referred to as the thir ...
- [IoLanguage]Io Programming Guide[转]
Io Programming Guide Introduction Perspective Getting Started Downloading Installing Binaries Ru ...
- Extension Methods "点"函数方法 扩展方法
原文发布时间为:2011-03-25 -- 来源于本人的百度文章 [由搬家工具导入] http://msdn.microsoft.com/en-us/library/bb383977.aspx 条件: ...
- Table View Programming Guide for iOS---(四)---Navigating a Data Hierarchy with Table Views
Navigating a Data Hierarchy with Table Views 导航数据表视图层次 A common use of table views—and one to which ...
- 【IOS笔记】View Programming Guide for iOS -1
原文:View Programming Guide for iOS View and Window Architecture Views and windows present your applic ...
- View Programming Guide for iOS_读书笔记[正在更新……]
原文:View Programming Guide for iOS 1 Introduction 先熟悉一下基本概念. Window Windows do not have any visible c ...
- 对Spark2.2.0文档的学习3-Spark Programming Guide
Spark Programming Guide Link:http://spark.apache.org/docs/2.2.0/rdd-programming-guide.html 每个Spark A ...
随机推荐
- 一篇文章告诉你如何使用EF CodeFirst做增删改查
一.修改数据 其实修改涉及的内容挺多的,是相对于其他操作来说比较繁琐.也是本文的重头戏. 虽然都是基础内容,但是也是值得细细品味的. 1.最简单直接的修改数据就是从数据库里检索出数据修改相应的字段即可 ...
- JS——轮播图高级版
需求: 1.页面需要小图标和箭头 2.定时器不断的滑动图片 3.在点击箭头时,图标和图片随即做出响应 核心思想: 1.往左移动到第一张的情况:在第二张移动到第一张时,这个动画效果完成之后,立刻将ul的 ...
- haproxy故障处理
1. haproxy 在配置健康检查的时候,默认没有配置页面检查 ,通过端口状态来检测.后端IIS web服务开始可能 是一个站点,或者采用了基于域名的配置方式,导致目前站点停了,后端主机不能被hap ...
- Java数值数学类
Java数值数学类 序号 方法与描述 1 xxxValue() 将 Number 对象转换为xxx数据类型的值并返回. 2 compareTo() 将number对象与参数比较. 3 equals ...
- 初学者怎么快速掌握Linux运维?
2018年里,Linux运维的职位数量和平均薪资水平仍然持续了去年的强劲增幅,比很多开发岗位涨的都快.从研究机构的数据来看,Linux职位数量和工资水平涨幅均在IT行业的前五之列,比去年的表现还要好一 ...
- Linux 开启443端口
1 在Linux终端输入指令: iptables -I INPUT -p tcp --dport 443 -j ACCEPT 2 回车之后继续输入指令,输入保存防火墙配置指令: service ...
- 什么是ACID
ACID是衡量事务的四个特性: 原子性(Atomicity,或称不可分割性) 一致性(Consistency) 隔离性(Isolation) 持久性(Durability) 原子性:原子性是指一个事务 ...
- python tkinter模块 创建窗口V1.2
先上图 代码如下 #-*-coding:utf-8-*- import os from tkinter import * root=Tk() root.title('执行窗口') "&quo ...
- koji
fedora koji https://koji.fedoraproject.org/koji/ centos cbs.centos.org/koji/
- Nginx配置文件的高亮显示设置
linux系统下vim或者vi编辑器默认是没有对nginx的语法高亮设置. 1.下载vi语法高亮配置到 ~/.vim/syntax,如果不存在则创建该目录,cd ~/.vim/syntax wget ...