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

来源:互联网 发布:大数据與1040 编辑:程序博客网 时间:2024/06/04 18:45

http://stackoverflow.com/a/846773/2177408

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?


Answer:

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

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.


0 0
原创粉丝点击