在 C# 中静态方法的形式参数中使用“this”关键字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/846766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use of "this" keyword in formal parameters for static methods in C#
提问by kpozin
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. Any insights?
this
在这种情况下,我无法找到关键字含义的解释。任何见解?
采纳答案by Preet Sangha
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. 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... .
扩展方法允许开发人员向现有 CLR 类型的公共契约添加新方法,而无需对其进行子类化或重新编译原始类型。扩展方法有助于将当今动态语言中流行的“鸭子类型”支持的灵活性与强类型语言的性能和编译时验证相结合。
扩展方法支持各种有用的场景,并帮助实现真正强大的 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 interfacesas stated below.
这允许构建如下所述的流畅接口。
回答by jpoh
I just learnt this myself the other day: the this keyword defines that method has being an extension of the class that proceeds it. 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).
前几天我自己才学会了这一点:this 关键字定义了该方法是执行它的类的扩展。因此,对于您的示例,MyClass 将有一个名为 Foo 的新扩展方法(它不接受任何参数并返回一个 int;它可以与任何其他公共方法一起使用)。
回答by JP Alioto
They are extension methods. Welcome to a whole new fluent world. :)
回答by James Wiseman
Scott Gu's quoted blog postexplains 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 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.
请注意上面的静态方法如何在字符串类型的第一个参数参数之前有一个“this”关键字。这告诉编译器应该将此特定扩展方法添加到“字符串”类型的对象中。在 IsValidEmailAddress() 方法实现中,我可以访问调用该方法的实际字符串实例的所有公共属性/方法/事件,并根据它是否为有效电子邮件返回 true/false。
回答by Olivier Jacot-Descombes
In addition to Preet Sangha's explanation:
Intellisense displays the extension methods with a blue arrow (e.g. in front of "Aggregate<>"):
除了 Preet Sangha 的解释:
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.
如果扩展方法位于与使用它们的代码不同的命名空间中,则扩展方法将出现并可用。
回答by dcarl661
"this" extends the next class in the parameter list
“this”扩展参数列表中的下一个类
So in the method signature below "this" extends "String". Line is passed to the function as a normal argument to the method. public static string[] SplitCsvLine(this String line)
所以在“this”下面的方法签名中扩展了“String”。Line 作为方法的普通参数传递给函数。public static string[] SplitCsvLine(这个字符串行)
In the above example "this" class is extending the built in "String" class.
在上面的例子中,“this”类扩展了内置的“String”类。