简体   繁体   English

在C#中静态方法的形式参数中使用“ this”关键字

[英]Use of “this” keyword in formal parameters for static methods in C#

I've come across several instances of C# code like the following: 我遇到了如下几个C#代码实例:

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. 在这种情况下,我无法找到this关键字的含义的解释。 Any insights? 有什么见解吗?

This is an extension method . 这是一种扩展方法 See here for an explanation . 请参阅此处以获取解释

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. 扩展方法使开发人员可以向现有CLR类型的公共合同添加新方法,而不必对其进行子类化或重新编译原始类型。 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. 扩展方法有助于将当今在动态语言中流行的“鸭子键入”支持的灵活性与强类型语言的性能和编译时验证相结合。

Extension Methods enable a variety of useful scenarios, and help make possible the really powerful LINQ query framework... . 扩展方法可实现各种有用的方案,并有助于实现真正强大的LINQ查询框架...。

it means that you can call 这意味着你可以打电话

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

rather than 而不是

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

This allows the construction of fluent interfaces as stated below. 这样可以构建如下所述的流畅接口

Scott Gu's quoted blog post explains it nicely. Scott Gu引用的博客文章对此进行了很好解释。

For me, the answer to the question is in the following statement in that post: 对我来说,该问题的答案在该帖子的以下声明中:

Note how the static method above has a "this" keyword before the first parameter argument of type string. 请注意,上面的静态方法如何在字符串类型的第一个参数实参之前具有“ this”关键字。 This tells the compiler that this particular Extension Method should be added to objects of type "string". 这告诉编译器应将此特殊扩展方法添加到“字符串”类型的对象中。 Within the IsValidEmailAddress() method implementation I can then access all of the public properties/methods/events of the actual string instance that the method is being called on, and return true/false depending on whether it is a valid email or not. 然后,在IsValidEmailAddress()方法实现中,我可以访问调用该方法的实际字符串实例的所有公共属性/方法/事件,并根据是否为有效电子邮件返回true / false。

In addition to Preet Sangha's explanation: 除了Preet Sangha的解释之外:
Intellisense displays the extension methods with a blue arrow (eg in front of "Aggregate<>"): Intellisense用蓝色箭头(例如,在“ Aggregate <>”前面)显示扩展方法:

在此处输入图片说明

You need a 你需要一个

using the.namespace.of.the.static.class.with.the.extension.methods;

for the extension methods to appear and to be available, if they are in a different namespace than the code using them. 如果扩展方法与使用它们的代码位于不同的名称空间中,则扩展方法将显示并可用。

They are extension methods . 它们是扩展方法 Welcome to a whole new fluent world . 欢迎来到一个全新的流利世界 :) :)

I just learnt this myself the other day: the this keyword defines that method has being an extension of the class that proceeds it. 前几天我才刚刚学到这一点:this关键字定义了该方法是对该方法进行扩展的类的扩展。 So for your example, MyClass will have a new extension method called Foo (which doesn't accept any parameter and returns an int; it can be used as with any other public method). 因此,对于您的示例,MyClass将具有一个称为Foo的新扩展方法(该扩展方法不接受任何参数并返回int;可以与任何其他公共方法一样使用)。

"this" extends the next class in the parameter list “ this”扩展了参数列表中的下一个类

So in the method signature below "this" extends "String". 因此在“ this”下面的方法签名中扩展了“ String”。 Line is passed to the function as a normal argument to the method. 将行作为方法的常规参数传递给函数。 public static string[] SplitCsvLine(this String line) 公共静态字符串[] SplitCsvLine(此字符串行)

In the above example "this" class is extending the built in "String" class. 在上面的示例中,“ this”类扩展了内置的“ String”类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM