的&QUOT使用;本"关键字在C#中的静态方法的形式参数 [英] Use of "this" keyword in formal parameters for static methods in C#

查看:120
本文介绍了的&QUOT使用;本"关键字在C#中的静态方法的形式参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到C#code类似下面的几个实例:

I've come across several instances of C# code like the following:

public static int Foo(this MyClass arg)

我一直没能找到什么样的这个关键词在这种情况下,意味着一个解释。任何见解?

I haven't been able to find an explanation of what the this keyword means in this case. Any insights?

推荐答案

这是一个扩展方法。看到这里的一个<一href=\"http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx\">explanation.

This is an extension method. See here for an explanation.

扩展方法允许开发人员添加新的方法向公众
  现有的CLR类型的合同,而无需子类,或
  重新编译原始类型。扩展方法帮助交融
  的鸭打字的灵活性支持动态语言中流行
  今天的性能和编译时验证
  强类型语言。

Extension methods allow developers to add new methods to the public contract of an existing CLR type, without having to sub-class it or recompile the original type. Extension Methods help blend the flexibility of "duck typing" support popular within dynamic languages today with the performance and compile-time validation of strongly-typed languages.

扩展方法使各种有用的方案,并帮助
  可能真正强大的LINQ查询框架...

Extension Methods enable a variety of useful scenarios, and help make possible the really powerful LINQ query framework... .

这意味着你可以调用

MyClass myClass = new MyClass();
int i = myClass.Foo();

而不是

MyClass myClass = new MyClass();
int i = Foo(myClass);

这允许流利的接口建设如下所述。

这篇关于的&QUOT使用;本&QUOT;关键字在C#中的静态方法的形式参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆